UVA 11827 Maximum GCD ungetc函数将ch字符重新退到输入流

本文介绍了一个使用C++实现的最大公约数(GCD)算法,并展示了如何处理输入流中的数字,包括如何利用ungetc函数回退字符到输入流中以实现灵活的输入解析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ungetc(ch,stdin);
将ch字符重新退到输入流里…好神奇的东西

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<queue>
#include<stack>
#include<string>
#include<vector>
#include<map>
#include<set>
using namespace std;
#define lowbit(x) (x&(-x))
typedef long long LL;
const int maxn = 100005;
const int inf=(1<<28)-1;
char str[maxn];
LL Num[maxn];
LL Gcd(LL a,LL b)
{
    if(!b) return a;
    return Gcd(b,a%b);
}
int main()
{
    int T;
    scanf("%d",&T);
    getchar();
    while(T--)
    {
        LL tmp=0,tot=0;
        char ch;
        while((ch=getchar())!='\n')
        {
            if(ch>='0'&&ch<='9')
            {
                ungetc(ch,stdin);
                scanf("%d",&Num[++tot]); 
            }
        }
        //for(int i=1;i<=tot;++i) printf("%lld ",Num[i]);printf("\n");
        LL Ans=0;
        for(int i=1;i<=tot;++i)
         for(int j=i+1;j<=tot;++j)
            Ans=max(Ans,Gcd(Num[i],Num[j]));
        printf("%lld\n",Ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值