Uva 11827 Maximum GCD(水题、读入杀)
2016-07-27 17:55
357 查看
题目链接:
http://acm.hust.edu.cn/vjudge/contest/70017#problem/V
题目大意:
读入一行数,求出所有数间最大的GCD值
分析:
根据GCD性质,N个数的GCD不大于N−1个数的GCD,所以最大GCD一定出现于两个数间,由于只有至多100个数,100组样例,O(N2)暴力,但是注意读入,一开始用字符串流一直WA,看了题解发现用C语言的字符读入就过了,玄学AC
代码:
http://acm.hust.edu.cn/vjudge/contest/70017#problem/V
题目大意:
读入一行数,求出所有数间最大的GCD值
分析:
根据GCD性质,N个数的GCD不大于N−1个数的GCD,所以最大GCD一定出现于两个数间,由于只有至多100个数,100组样例,O(N2)暴力,但是注意读入,一开始用字符串流一直WA,看了题解发现用C语言的字符读入就过了,玄学AC
代码:
#include <iostream> #include <cstdio> #include <cstdlib> #include <sstream> #include <cstring> #include <string> #include <vector> #include <cmath> #include <set> #define MOD 1000000007 using namespace std; typedef unsigned long long ull; typedef long long ll; /*---------------------------head files----------------------------------*/ bool num[150000]; void fd() { for (int i = 2 ; i <= 100000 ; i ++) { if (!num[i]) { for (int j = 2*i ; j <= 100000 ; j += i) num[j] = true; } } num[1]=true; num[2]=true; } inline ll max(ll a , ll b) { return a>b? a: b; } ll gcd (ll a , ll b) { return b==0? a : gcd(b,a%b); } int arr[6666]; int main() { string str; ios::sync_with_stdio(false); ll n,a; char buf; scanf("%d",&n); while (getchar() != '\n'); while (n--) { int cnt=0; ll maxx=0; while ((buf = getchar()) != '\n') if (buf >= '0' && buf <= '9') { ungetc(buf,stdin); scanf("%d",&arr[cnt ++]); } for (int i = 0 ; i < cnt ; i ++) { for (int j = i+1; j <cnt ; j ++) { if (i!=j) maxx = max(maxx, gcd(arr[i],arr[j])); } } cout<<maxx<<endl; } }
相关文章推荐
- 详解iOS多线程GCD的使用
- 详解IOS中GCD的使用
- 详解iOS中多线程app开发的GCD队列的使用
- gcd
- OC多线程
- 多线程应该知道的那几件事 GCD NSThread NSOperation
- 多线程编程4 - GCD
- 多线程学习资源
- block && Grand Central Dispatch
- GCD使用指南
- 1.10055 - Hashmat the brave warrior
- 2.10071 - Back to High School Physics
- 3.458 - The Decoder
- 4.694 - The Collatz Sequence
- 6.494 - Kindergarten Counting Game
- 7.490 - Rotating Sentences
- 8.414 - Machined Surfaces
- 9.488 - Triangle Wave
- A.457 - Linear Cellular Automata
- B.489 - Hangman Judge