UVA 11827 - Maximum GCD

本文介绍了一道数论题目,重点在于一种特殊的输入方法。通过使用stringstream处理输入流,实现了从一串字符串中读取多个整数,并计算这些整数间最大公约数的最大值。
一道挺简单的数论题,但是数论不是主要的问题,输入是主要的问题,使用了一个比较奇特的输入方法,使读入的一串字符串形成一个输入流
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <functional>
#include <cstdio>
#include <queue>
#include <map>
#include <algorithm>
#include <stack>
#include <utility>
#include <set>
#include<sstream>
typedef unsigned long long ll;
using namespace std;
const int mx = 106;
int n,num[mx];
string s;
int gcd(int a, int b)
{
    return a%b?gcd(b, a%b):b;
}
char a;
int main ()
{
    int n,cnt,i,j,k,ans;
    scanf("%d\n",&n);
    while (n --)
    {
        ans = 1;
        cnt = 0;
        getline(cin, s);
        stringstream ss(s);
        cnt = 0;
        while (ss >> num[cnt]) // 从ss中取数
            cnt++;
        for(i = 0; i < cnt; i++)
            for(j = i + 1; j < cnt; j++)
                ans = max(ans,gcd(num[i],num[j]));
        printf("%d\n",ans);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值