A long time not to write blog, can not be decadent.
The data range of this problem is very small, n<=100. So direct violence solves the problem, and then we find that the read-in length of the question is not required, then we need to apply the StringStream string input stream
Topic:
Given the N integers, you has to nd the maximum GCD (greatest common divisor) of every possible
Pair of these integers.
Input
The RST line of input is a integer N (1 < N <) that determines the number of the test cases.
The following n lines is the n test cases. Each test case contains M (1 < m <) positive
Integers that you had 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
Code:
#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);//Read in a string type of data StringStream SS (s);//defines an input stream n = 0; while (SS >> Num[n])//convert string to int type data, encounter space and enter to end conversion ++n; printf ("%d\n", Cal ()); } return 0;}
UVA 11827 Maximum GCD (read-in technique, stringstream use)