Maximum GCD 读入
来源:互联网 发布:淘宝如何设置子账号 编辑:程序博客网 时间:2024/06/07 07:06
Maximum GCD
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
题意: 给你 n 个数求GCD();
关键在于对于每个测试样例,你不知道有多少个,有个很好的读入技巧。
ungetc(c,stdin) : 将你读到的字符回退到输入流中;
完美的解决了这个问题,但是我的codeblock 上竟然运行不了,不管了,反正能A,而且代码减少了很多。
代码:
#include<iostream>#include<cstdio>#define maxn 1010using namespace std;int gcd(int a,int b){ if(b==0) return a; return gcd(b,a%b);}int main(){ int n; int a[maxn]; int cnt; char c; scanf("%d",&n); while(getchar()!='\n'); while(n--) { cnt=0; while((c=getchar())!='\n') { if(c>='0'&&c<='9') { ungetc(c,stdin); scanf("%d",&a[cnt++]); } } int mx=-1; for(int i=0;i<cnt-1;i++) for(int j=i+1;j<cnt;j++) { int tt=gcd(a[i],a[j]); if(tt>mx) mx=tt; } cout<<mx<<endl; } return 0;}
阅读全文
0 0
- Maximum GCD 读入
- UVa 11827 Maximum GCD (gcd&读入技巧)
- UVA 11827 Maximum GCD gcd 读入技巧
- Maximum GCD - UVa 11827 最大公约数 读入有坑
- Uva 11827 Maximum GCD(水题、读入杀)
- Maximum GCD
- Maximum GCD
- UVA 11827 Maximum GCD gcd
- UVa 11827 Maximum GCD
- UVA 11827-Maximum GCD
- UVa 11827 - Maximum GCD
- UVA 11827 - Maximum GCD
- UVA 11827 Maximum GCD
- UVa 11827 Maximum GCD
- UVA 11827Maximum GCD
- Maximum GCD [水题]
- Maximum GCD UVA
- UVA 11827 Maximum GCD
- JZOJ2017.08.05 C组
- 【凸包 Graham法 极角排序】poj 2007 Scrambled Polygon
- 1.在win10中搭建Java环境
- 练习 2-6 编写一个函数 setbits(x, p, n, y),该函数返回对x执行下列操作后的结果值:将x中从第p位开始的n个(二进制)位设置为y中最右边n位的值,x的其余各位保持不变。
- PHP基础教程-12 课后作业02
- Maximum GCD 读入
- 关于listview或者recyclerview里面的edittext数据保存问题
- windows安装gensim
- logstash(一):搭建syslog测试环境
- 机器学习笔记——广义线性模型(Generalized Linear Models, GLM)
- Java 用两个一维数组实现双色球彩票
- 练习 2-7 编写一个函数 invert(x, p, n),该函数返回对x 执行下列操作后的结 果值:将x中从第p位开始的n个(二进制)位求反(即,1 变成0,0 变成1),x的其余各位保持不变。
- 贪心,递归,动态规划,及分治算法之间的区别和联系(三)
- ACM常见错误