Given the N integers, you have to find the maximum GCD (greatest common divisor) of every possiblepair 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) positiveintegers 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

思路:

暴力gcd,之前小紫看到的stringstream可以拿出来用了,很方便啊,这里就用到了这个技巧

代码:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<queue>
#include<cmath>
#include<string>
#include<map>
#include<stack>
#include<set>
#include<vector>
#include<iostream>
#include<algorithm>
#include<sstream>
#define INF 0x3f3f3f3f
#define ll long long
const int N=16005;
const ll MOD=998244353;
using namespace std;
ll gcd(ll a,ll b){
return b==0? a:gcd(b,a%b);
}
int main(){
int n;
ll num[110],x,MAX=-100000000;
string s;
scanf("%d",&n);
getchar();
while(n--){
getline(cin,s);
int sum=0;
MAX=-100000000;
stringstream ss(s);
while(ss>>x){
num[sum++]=x;
}
for(int i=0;i<sum;i++){
for(int j=i+1;j<sum;j++){
MAX=max(MAX,gcd(num[i],num[j]));
}
}
cout<<MAX<<endl;
}
return 0;
}

Maximum GCD (stringstream)题解的更多相关文章

  1. UVA 11827 Maximum GCD

    F - Maximum GCD Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Given the ...

  2. 邝斌带你飞之数论专题--Maximum GCD UVA - 11827

    Given the N integers, you have to find the maximum GCD (greatest common divisor) of every possible p ...

  3. Maximum GCD(UVA 11827)

    Problem:Given the N integers, you have to find the maximum GCD (greatest common divisor) of every po ...

  4. Codeforces Round #651 (Div. 2) A Maximum GCD、B GCD Compression、C Number Game、D Odd-Even Subsequence

    A. Maximum GCD 题意: t组输入,然后输入一个n,让你在区间[1,n]之间找出来两个不相等的数a,b.求出来gcd(a,b)(也就是a,b最大公约数).让你求出来最大的gcd(a,b)是 ...

  5. Maximum GCD(fgets读入)

    Maximum GCD https://vjudge.net/contest/288520#problem/V Given the N integers, you have to find the m ...

  6. UVA - 11827 - Maximum GCD,10200 - Prime Time (数学)

    两个暴力题.. 题目传送:11827 Maximum GCD AC代码: #include <map> #include <set> #include <cmath> ...

  7. UVA11827 Maximum GCD

    /* UVA11827 Maximum GCD https://vjudge.net/contest/153365#problem/V 数论 gcd 水题,然而读入比较坑 * */ #include ...

  8. Codeforces Round #651 (Div. 2) A. Maximum GCD(数论)

    题目链接:https://codeforces.com/contest/1370/problem/A 题意 有 $n$ 个数大小分别为 $1$ 到 $n$,找出两个数间最大的 $gcd$ . 题解 若 ...

  9. Codeforces Round #651 (Div. 2) A. Maximum GCD (思维)

    题意:在\(1\)~\(n\)中找两个不相等的数使得他们的\(gcd\)最大. 题解:水题,如果\(n\)是偶数,那么一定取\(n\)和\(n/2\),\(n\)是奇数的话,取\(n-1\)和\((n ...

随机推荐

  1. MySql关联子查询

    mysql有时候把子查询优化的很差,最差的情景就是 在where子句中使用in. -----<高性能mysql第二版>4.4.1

  2. Catch That Cow--POJ3278

    Description Farmer John has been informed of the location of a fugitive cow and wants to catch her i ...

  3. Python开发【笔记】:what?进程queue还能生产出线程!

    进程queue底层用线程传输数据 import threading import multiprocessing def main(): queue = multiprocessing.Queue() ...

  4. java连接数据库时的报错

    //java连接数据库时的报错 1 package Java数据库编程; import java.sql.DriverManager; import java.sql.SQLException; im ...

  5. SQL SERVER - 谁更改了SQL登录密码?

    转自:https://blog.sqlauthority.com/2016/04/03/sql-server-changed-password-sql-login-interview-question ...

  6. Java编程实现获取本机IP和计算机名的功能

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/wangshuxuncom/article/details/35990847 import java. ...

  7. 使用 Mirantis Fuel9.0 部署 OpenStack M

    Mirantis Fuel 9 可以实现部署OpenStack M版本web化,管理员只需简单规划就能部署复杂的openstack 组件 安装Fuel9.0 下载官方IOS镜像 https://www ...

  8. ID和Name

    ID和Name都可以用来标识一个标记,Javascript分别有两个方法getElementById和getElementByName来定位Dom节点. 区别如下: 1.我们知道在网页做Post提交时 ...

  9. weka源代码-总述

    分类: 所有的分类器都继承自抽象类AbstractClassifier而AbstractClassifier继承自接口Classifier.集成关系如下图所示: 而类Classifier中主要包含以下 ...

  10. EM算法小结

    一.什么是EM算法? EM算法是机器学习中一个很重要的算法,即期望最大化算法,主要包括以下两个步骤: E步骤:estimate the expected values M步骤:re-estimate ...