UVA 11827 Maximum GCD(读入技巧,stringstream的使用)
2015-08-17 12:16
495 查看
好久没来写blog了,不能颓了。
这道题的数据范围很小,n<=100.所以直接暴力就可以解决问题,然后,我们发现这个题的读入长度并没有要求,那么我们就需要应用stringstream这个字符串输入流
题目:
Given the N integers, you have to nd the maximum GCD (greatest common divisor) of every possible
pair of these integers.
Input
The rst 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 nd 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<=100.所以直接暴力就可以解决问题,然后,我们发现这个题的读入长度并没有要求,那么我们就需要应用stringstream这个字符串输入流
题目:
Given the N integers, you have to nd the maximum GCD (greatest common divisor) of every possible
pair of these integers.
Input
The rst 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 nd 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<cstdio> #include<iostream> #include<sstream> #include<string> using namespace std; int num[100], n; string s; int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } int cal() { int i, j, maxn = 0; for (i = 0; i < n - 1; ++i) for (j = i + 1; j < n; ++j) maxn = max(maxn, gcd(num[i], num[j])); return maxn; } int main() { int t; scanf("%d\n", &t); while (t--) { getline(cin, s);//读入的是string类型的数据 stringstream ss(s);//定义了一个输入流 n = 0; while (ss >> num )//将字符串转化为int类型的数据,遇到空格和回车就结束转换 ++n; printf("%d\n", cal()); } return 0; }
相关文章推荐
- Android 倒计时
- 【Linux学习】epoll详解
- 算法竞赛入门经典:第五章 基础题目选解 5.10 因子和阶乘
- Android HAL JNI实例解析-led
- 服务器上配置多个tomcat
- 从设计到实现,一步步教你实现Android-Universal-ImageLoader-辅助类
- js+cookie 购物车
- linux 下如何给火狐安装flash插件(常用命令cd cp tar 实践)
- 【动态规划】mr368-教主种树
- unicode下各种类型转换CString、string
- Python中map,reduce,filter和sorted函数的使用方法
- Win7打开文件安全警告取消方法
- bzoj 1030: [JSOI2007]文本生成器 (ac自动机上的dp)
- 黑马程序员——————java基础
- 论 Angular的混乱
- php.ini文件中的include_path设置
- 【动态规划+高精度】mr360-定长不下降子序列
- Java中的static和final关键字
- 使用CSS将图片转换成黑白(灰色、置灰)
- NOIP2010原题考试总结