前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Maximum GCD(UVA 11827)

Maximum GCD(UVA 11827)

作者头像
Lokinli
发布2023-03-09 16:32:36
2630
发布2023-03-09 16:32:36
举报
文章被收录于专栏:以终为始以终为始

Problem:Given the N integers, you have to find the maximum GCD (greatest common divisor) of every possible pair of these integers.

Input :The first 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 find 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

题解:读入的时候处理一下,可以直接读入一个字符串,然后把数再按十进制还原存到数组中,或者直接用ungetc来退回一下。

代码语言:javascript
复制
#include <bits/stdc++.h>
using namespace std;
int a[150];
int main()
{
    int n, maxx = -1;
    char op;
    while(~scanf("%d",&n))
    {
        while(n --)
        {
            maxx = -1;
            int i = 0;
            while(1)
            {
                scanf("%d",&a[i++]);
                while((op=getchar())==' '); // 如果是空格的话用ungetc退格
                ungetc(op, stdin);
                if(op == '\n') break;
            }
            for(int j = 0; j < i; j ++)
            {
                for(int k = j + 1; k < i; k ++)
                {
                    if(__gcd(a[j],a[k]) > maxx) maxx = __gcd(a[j],a[k]);
                }
            }
            printf("%d\n",maxx);
        }
    }
    return 0;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-08-25,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档