Maximum GCD(fgets读入)
Maximum GCD
https://vjudge.net/contest/288520#problem/V
Given the N integers, you have to find the maximum GCD (greatest common divisor) of every possible pair of these integers.
Input
The first line of input is an integer N (1 < N < 100) that determines the number of test cases. The following N lines are the N test cases. Each test case contains M (1 < M < 100) positive integers that you have to find the maximum of GCD.
Output
For each test case show the maximum GCD of every possible pair.
Sample Input
3
10 20 30
40 7 5 12
125 15 25
Sample Output
20
1
25
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define lson l,mid,rt<<1 4 #define rson mid+1,r,rt<<1|1 5 #define sqr(x) ((x)*(x)) 6 #define pb push_back 7 #define eb emplace_back 8 #define maxn 10000005 9 #define eps 1e-8 10 #define pi acos(-1.0) 11 #define rep(k,i,j) for(int k=i;k<j;k++) 12 typedef long long ll; 13 typedef pair<int,int> pii; 14 typedef pair<long long,int>pli; 15 typedef pair<int,char> pic; 16 typedef pair<pair<int,string>,pii> ppp; 17 typedef unsigned long long ull; 18 const long long mod=1e9+7; 19 const double oula=0.57721566490153286060651209; 20 using namespace std; 21 22 int n; 23 char str[10005]; 24 vector<int>ve; 25 26 int main(){ 27 scanf("%d%*c",&n); 28 for(int i=1;i<=n;i++){ 29 fgets(str,sizeof(str),stdin); 30 ve.clear(); 31 int co=0; 32 int len=strlen(str); 33 for(int j=0;j<len;j++){ 34 if(str[j]>='0'&&str[j]<='9'){ 35 co=co*10+str[j]-'0'; 36 } 37 else{ 38 if(co) 39 ve.pb(co); 40 co=0; 41 } 42 } 43 if(co){ 44 ve.pb(co); 45 } 46 int ans=1; 47 for(int i=0;i<ve.size();i++){ 48 for(int j=i+1;j<ve.size();j++){ 49 ans=max(ans,__gcd(ve[i],ve[j])); 50 } 51 } 52 printf("%d\n",ans); 53 } 54 }
posted on 2019-03-19 20:55 Fighting_sh 阅读(158) 评论(0) 编辑 收藏 举报