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

数据不大,直接暴力,这里学习的是这种输入方法

#include<map>
#include<queue>
#include<stack>
#include<vector>
#include<math.h>
#include<cstdio>
#include<sstream>
#include<numeric>//STL数值算法头文件
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<functional>//模板类头文件
using namespace std; const int INF=1e9+7;
const int maxn=1010;
typedef long long ll; int t;
string str;
int a[maxn]; int gcd(int a,int b)
{
return b?gcd(b,a%b):a;
} int main()
{
scanf("%d\n",&t);
while(t--)
{
int i,j,maxx=0;
getline(cin,str);
stringstream ss(str);
int n=0;
while(ss>>a[n])
n++;
for(i=0; i<n-1; i++)
for(j=i+1; j<n; j++)
maxx=max(maxx,gcd(a[i],a[j]));
printf("%d\n",maxx);
}
return 0;
} //方法二
int gcd(int a,int b)
{
return b?gcd(b,a%b):a;
}
int main()
{
int T;
int a[105];
char c;
scanf("%d",&T);
while (getchar() != '\n');
while(T--)
{
int cnt=0;
while((c=getchar())!='\n')
{
if(c>='0' && c<='9')
{
ungetc(c,stdin);//将字符c退回到输入流中
scanf("%d",&a[cnt++]);
}
}
int max=0;
for(int i=0; i<cnt-1; i++)
{
for(int j=i+1; j<cnt; j++)
{
int t=gcd(a[i],a[j]);
if(t>max) max=t;
}
}
printf("%d\n",max);
}
return 0;
}

邝斌带你飞之数论专题--Maximum GCD UVA - 11827的更多相关文章

  1. Kuangbin 带你飞-基础计算几何专题 题解

    专题基本全都是模版应用.贴一下模版 平面最近点对 const double INF = 1e16; ; struct Point { int x,y; int type; }; double dist ...

  2. Kuangbin 带你飞-线段树专题 题解

    HDU 1166 敌兵布阵 单调更新区间查询和 #include <map> #include <set> #include <list> #include < ...

  3. 【kuangbin带你飞】 MST专题

    唉,被班级合唱和复变考试搞得心力交瘁.新算法学不进去,更新下吧 A - Til the Cows Come Home The Head Elder of the tropical island of ...

  4. [kuangbin带你飞]专题1-23题目清单总结

    [kuangbin带你飞]专题1-23 专题一 简单搜索 POJ 1321 棋盘问题POJ 2251 Dungeon MasterPOJ 3278 Catch That CowPOJ 3279 Fli ...

  5. ACM--[kuangbin带你飞]--专题1-23

    专题一 简单搜索 POJ 1321 棋盘问题POJ 2251 Dungeon MasterPOJ 3278 Catch That CowPOJ 3279 FliptilePOJ 1426 Find T ...

  6. KUANGBIN带你飞

    KUANGBIN带你飞 全专题整理 https://www.cnblogs.com/slzk/articles/7402292.html 专题一 简单搜索 POJ 1321 棋盘问题    //201 ...

  7. 「kuangbin带你飞」专题十四 数论基础

    layout: post title: 「kuangbin带你飞」专题十四 数论基础 author: "luowentaoaa" catalog: true tags: mathj ...

  8. 【算法系列学习三】[kuangbin带你飞]专题二 搜索进阶 之 A-Eight 反向bfs打表和康拓展开

    [kuangbin带你飞]专题二 搜索进阶 之 A-Eight 这是一道经典的八数码问题.首先,简单介绍一下八数码问题: 八数码问题也称为九宫问题.在3×3的棋盘,摆有八个棋子,每个棋子上标有1至8的 ...

  9. 「kuangbin带你飞」专题二十 斜率DP

    layout: post title: 「kuangbin带你飞」专题二十 斜率DP author: "luowentaoaa" catalog: true tags: mathj ...

随机推荐

  1. 2015/11/1用Python写游戏,pygame入门(1):pygame的安装

    这两天学习数据结构和算法,有时感觉并不如直接做项目来的有趣.刚刚学完python的基本使用,现在刚好趁热打铁做个小项目. 由于本人一直很想制作一款游戏,就想使用Python制作一个基础的游戏.搜了一下 ...

  2. Hadoop 面试总结

    1.简要描述如何安装配置一个开源的hadoop,只描述即可,列出完整步骤. a.创建一个用户和用户组,用来管理hadoop项目 b.修改确定ip地址:vim /etc/sysconfig/networ ...

  3. IntelliJ Idea key shortcuts

    >Default explaination Official IntelliJ Idea 常用快捷键列表 Shortcuts Ctrl+Shift + Enter,语句完成 "!&qu ...

  4. CodeForces 990C

    Description A bracket sequence is a string containing only characters "(" and ")" ...

  5. 【译】DTD - Entities

    原文:DTD - Entities 实体用于定义XML文档中特殊字符的快捷方式. 实体主要有四种类型: 内置实体(Built-in entities) 字符实体(Character entities) ...

  6. Tornado/Python 学习笔记(二)

    部分ssrpc.py代码分析 -- 服务端: 1 #!/usr/bin/python3 2 3 from xmlrpc.client import Fault, dumps, loads 4 impo ...

  7. docker ubuntu容器更换阿里源(转)

    问题:使用docker 利用下载的ubuntu镜像启动容器时,使用的源下载更新软件的速度较慢. 解决这个问题的方法是跟新ubuntu容器的源 示例:以ubuntu为基础镜像 启动一个名称为 test0 ...

  8. Linux进程调度原理【转】

    转自:http://www.cnblogs.com/zhaoyl/archive/2012/09/04/2671156.html Linux进程调度的目标 1.高效性:高效意味着在相同的时间下要完成更 ...

  9. Mutex, semaphore, spinlock的深度解析

    Mutex是一把钥匙,一个人拿了就可进入一个房间,出来的时候把钥匙交给队列的第一个.一般的用法是用于串行化对critical section代码的访问,保证这段代码不会被并行的运行. Semaphor ...

  10. xargs -i 和-I 的区别【转】

    xargs与find经常结合来进行文件操作,平时删日志的时候只是习惯的去删除,比如  # find . -type f -name "*.log" | xargs rm -rf * ...