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;
    }
  • 相关阅读:
    如何让json_decode解码变的更加强壮
    scp命令
    Centos7安装postgresql
    ubuntu安装Java环境
    ubuntu开放端口
    VMware安装Ubuntu
    redis主从安装
    redis主从学习
    redis集群学习
    C++ 09 顺序容器
  • 原文地址:https://www.cnblogs.com/Ritchie/p/5300221.html
Copyright © 2011-2022 走看看