GCD is Funny
Accepts: 524
Time Limit: 4000/2000 MS (Java/Others)
Memory Limit: 65536/65536 K (Java/Others)
1 2 2 1 2 3
题意很简单 就是找n中任意两个数的最大最大公约数,我最开始选择用数组去存,结果超时了 ,导致wa了三遍,
后来选择用map去存
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<map> #include<algorithm> typedef long long ll; using namespace std; int test[1006]; int test2[1006]; int GCD(int a,int b) { if(a < b) int temp = a, a = b, b = temp; if(b == 0) return a; return GCD(b,a%b); } int main() { int t; int i,j; int n; scanf("%d",&t); map<int,int>test2; while(t--) { scanf("%d",&n); for(i=1;i<=n;i++) scanf("%d",&test[i]); int tt; for(i=1;i<n;i++) { for(j=i+1;j<=n;j++) { tt=GCD(test[i],test[j]); test2[tt]=1; } } map<int,int>::iterator it; int flag=1; for(it=test2.begin();it!=test2.end();it++) { if(flag==1) printf("%d",it->first); else printf(" %d",it->first); flag=0; } test2.clear(); printf(" "); } }