GCD(按行读入整数 stringstream的应用) - Maximum GCD - UVA 11827

GCD(按行读入整数 stringstream的应用) - Maximum GCD - UVA 11827

题意:

求 给 定 的 n 个 数 中 , 两 两 之 间 的 最 大 公 约 数 的 最 大 值 。 求给定的n个数中,两两之间的最大公约数的最大值。 n

输入:

T ( 1 < T < 100 ) 组 测 试 数 据 , T(1 < T < 100)组测试数据, T(1<T<100)

每 组 包 括 一 行 正 整 数 数 量 在 ( 1 , 100 ) 内 。 每组包括一行正整数数量在(1,100)内 。 (1,100)

输出:

一 个 正 整 数 , 表 示 答 案 。 一个正整数,表示答案。

Sample Input

3
10 20 30 40
7 5 12
125 15 25

Sample Output

20
1
25

分析:

由 于 每 组 数 据 的 数 都 小 于 100 个 , 所 以 我 们 可 以 直 接 暴 力 乱 搞 。 由于每组数据的数都小于100个,所以我们可以直接暴力乱搞。 100

重 点 在 于 每 组 数 据 的 数 如 何 读 入 。 重点在于每组数据的数如何读入。

这 里 用 到 s t r i n g s t r e a m 类 , 配 合 g e t l i n e 函 数 整 行 读 取 字 符 串 , 再 通 过 s t r i n g s t r e a m 类 转 化 为 整 型 数 。 这里用到stringstream类,配合getline函数整行读取字符串,再通过stringstream类转化为整型数。 stringstreamgetlinestringstream

s t r i n g s t r e a m 在 头 文 件 s s t r e a m 中 。 stringstream在头文件sstream中。 stringstreamsstream

代码:

#include <sstream>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int N=110;

int T;
string line;
int num[N], idx;

int gcd(int a,int b)
{
    return b ? gcd(b,a%b) : a;
}

int main()
{
    cin>>T;
    getline(cin,line);
    while(T--)
    {
        idx=0;

        getline(cin,line);
        stringstream tmp;
        tmp<<line;
        while(tmp>>num[idx++]); 
        idx--;  //最后一个空字符

        int ans=0;
        for(int i=0;i<idx;i++)
            for(int j=i+1;j<idx;j++)
                ans=max(ans,gcd(num[i],num[j]));

        cout<<ans<<endl;
    }
    return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值