Problem: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

题解:读入的时候处理一下,可以直接读入一个字符串,然后把数再按十进制还原存到数组中,或者直接用ungetc来退回一下。

#include <bits/stdc++.h>
using namespace std;
int a[150];
int main()
{
int n, maxx = -1;
char op;
while(~scanf("%d",&n))
{
while(n --)
{
maxx = -1;
int i = 0;
while(1)
{
scanf("%d",&a[i++]);
while((op=getchar())==' '); // 如果是空格的话用ungetc退格
ungetc(op, stdin);
if(op == '\n') break;
}
for(int j = 0; j < i; j ++)
{
for(int k = j + 1; k < i; k ++)
{
if(__gcd(a[j],a[k]) > maxx) maxx = __gcd(a[j],a[k]);
}
}
printf("%d\n",maxx);
}
}
return 0;
}

Maximum GCD(UVA 11827)的更多相关文章

  1. hdu 1695 GCD(莫比乌斯反演)

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  2. 【BZOJ2820】YY的GCD(莫比乌斯反演)

    [BZOJ2820]YY的GCD(莫比乌斯反演) 题面 讨厌权限题!!!提供洛谷题面 题解 单次询问\(O(n)\)是做过的一模一样的题目 但是现在很显然不行了, 于是继续推 \[ans=\sum_{ ...

  3. 【BZOJ2818】Gcd(莫比乌斯反演)

    [BZOJ2818]Gcd(莫比乌斯反演) 题面 Description 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的 数对(x,y)有多少对. Input 一个整数N Ou ...

  4. 【HDU1695】GCD(莫比乌斯反演)

    [HDU1695]GCD(莫比乌斯反演) 题面 题目大意 求\(a<=x<=b,c<=y<=d\) 且\(gcd(x,y)=k\)的无序数对的个数 其中,你可以假定\(a=c= ...

  5. 求GCD(最大公约数)的两种方式

    求GCD(最大公约数)的两种方式 这篇随笔讲解C++语言程序设计与应用中求GCD(最大公约数,下文使用GCD代替)的两种常用方式:更相减损法和辗转相除法,前提要求是具有小学数学的基本素养,知道GCD是 ...

  6. Codeforces C. Maximum Value(枚举二分)

    题目描述: Maximum Value time limit per test 1 second memory limit per test 256 megabytes input standard ...

  7. Maximum GCD (stringstream)题解

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

  8. Codeforces 1092F Tree with Maximum Cost(树形DP)

    题目链接:Tree with Maximum Cost 题意:给定一棵树,树上每个顶点都有属性值ai,树的边权为1,求$\sum\limits_{i = 1}^{n} dist(i, v) \cdot ...

  9. P2257 YY的GCD(莫比乌斯反演)

    第一次做莫比乌斯反演,推式子真是快乐的很啊(棒读) 前置 若函数\(F(n)\)和\(f(d)\)存在以下关系 \[ F(n)=\sum_{n|d}f(d) \] 则可以推出 \[ f(n)=\sum ...

随机推荐

  1. 关于@service、@controller和@transactional 在spring中的位置说明

    Spring容器优先加载由ServletContextListener(对应applicationContext.xml)产生的父容器,而SpringMVC(对应mvc_dispatcher_serv ...

  2. 怎样退出mysql命令行

    使用: mysql -u root -p 进入 mysql 命令号以后, 如果想退出, 可以使用: quit 命令, 如下: mysql -u root -p quit;

  3. Attribute自定义特性+Asp.net MVC中的filter详解

    转载自:http://blog.csdn.net/wangyy130/article/details/44241957 一.filter简介 在了解自定义特性前,先引入一个概念filter,它是MVC ...

  4. ItemsControl使用1

    <ItemsControl ItemsSource="{Binding DataItemsSource}"> //绑定一个List <ItemsControl.I ...

  5. vue添加图片

    首先开始创建一个 static 文件夹用来保存图片 去 setting 里面进行配置 MEDIA_ROOT = os.path.join(BASE_DIR,'media') #前面大写的是死格式,尽量 ...

  6. [转载]IMDB文件格式

    [转载]IMDB文件格式 来源:LMDB的全称是Lightning Memory-Mapped Database,闪电般的内存映射数据库.它文件结构简单,一个文件夹,里面一个数据文件,一个锁文件.数据 ...

  7. Ubuntu18.04通过网线共享网络

    Ubuntu18.04通过网线共享网络 这几天要给实验室一个新电脑装系统,但是实验室路由器好像有点问题,所以决定共享我的笔记本的网络,但是搜了很多教程都是基于Ubuntu16.04的,而Ubuntu1 ...

  8. 【SpringBoot】入门程序和机制分析

    一.初建项目 首先要导入SpringBoot的Maven依赖 <!-- Inherit defaults from Spring Boot --> <!-- 这是SpringBoot ...

  9. GitHub新手使用篇

    如何使用GitHub 未完结 目录: ISSUE总汇总: Issue1:GitHub的注册和使用? 答:(1)注册GitHub :https://github.com/.需要填用户名.邮箱.密码,值得 ...

  10. Flutter——Column组件(垂直布局组件)

    Column组件的常用属性 属性 说明 mainAxisAlignment 主轴的排序方式 crossAxisAlignment 次轴的排序方式 children 组件子元素 import 'pack ...