zoukankan      html  css  js  c++  java
  • UVA 11827 Maximum GCD

    F - Maximum GCD

    Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

    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 <iostream>
    #include <string.h>
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    #include <sstream>
    using namespace std;
    int gcd (int a,int b)
    {
        if(b==0)
        return a;
        else
        return gcd(b,a%b);
    }
    int main()
    {
        int i,j,n,t,a[105];
        cin>>t;
        getchar();
        while(t--)
        {
            //getchar();
            memset(a,0,sizeof(a));
            string str;
            getline(cin,str);
            stringstream stream(str);
            int n=0;
            while(stream>>a[n])
            {
                n++;
            }
            int ans=1;
            for(i=0;i<n;i++)
            for(j=i+1;j<n;j++)
            ans=max(gcd(a[i],a[j]),ans);
            cout<<ans<<endl;
        }
        return 0;
    }
  • 相关阅读:
    51nod 1117 聪明的木匠:哈夫曼树
    51nod 1010 只包含因子2 3 5的数
    51nod 2636 卡车加油
    51nod 2989 组合数
    51nod 2652 阶乘0的数量 V2
    51nod 1103 N的倍数
    51nod 2489 小b和灯泡
    51nod 1003 阶乘后面0的数量
    51nod 2122 分解质因数
    javascript中的setter和getter
  • 原文地址:https://www.cnblogs.com/Ritchie/p/5300221.html
Copyright © 2011-2022 走看看