UVA 11827 Maximum GCD (read-in technique, stringstream use)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.