一道水题

真的是水题

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

这题明显的暴力解决,简直就是送分题。

但是我tle了,后面才发现原来要用到c++输入流。一行行输入,不过为什么不能一个一个输入直到遇见空格,这窝也不大清楚了。

用的是c++里边的string

这个是包含在头文件<cstring>里边的

可以string str;定义一个字符串,而且神奇的是字符串可以相加,这个到时得去看看,还有就是另一个头文件<sstream>,这个应该是string里边的输入流,总之可以定义

stringstream stream(str);

这样就可以将一行做为一个流来输入,while(cin>>s[i])。

实现了一行一行输入。

不说了,直接上代码

#include <stdio.h>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <sstream>
#define INF 1e9

using namespace std;
int t;
int s[105],ans;
int gcd(int a,int b)
{
    return b==0?a:gcd(b,a%b);
}
int main(void)
{
    scanf("%d",&t);
    getchar();
    while (t--)
    {
        int i = 0;
        string str;
        getline(cin,str);
        stringstream stream(str);
        while (stream>>s[i])
        {
            i++;
        }
        ans = 0;
        for (int j = 0;j < i;j++)
        {
            for (int k = 0;k < i;k++)
            {
                if(k==j)
                    continue;
                //cout<<gcd(s[j],s[k])<<endl;
                ans = max(ans,gcd(s[j],s[k]));
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

睡觉 睡觉 安


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值