HDU 5050 高精度二进制数的最大公约数(C++ && Java)

C++版:

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define LL long long
#define FOR(i,x,y) for(int i=x;i<=y;i++)
#define rFOR(i,x,y) for(int i=x;i>=y;i--)
#define rep(i,x,y) for(int i=x;i<y;i++)
using namespace std;
const int maxn = 10000 + 10;
struct Bignum
{
    int a[maxn];
    int n;
    void input(char str[])
    {
        n = strlen(str);
        rep(i,0,n) a[i] = str[n-i-1] - '0';
    }
    void output()
    {
        rFOR(i,n-1,0)printf("%d",a[i]);
    }
    bool operator < (const Bignum& rhs) const
    {
        if(n < rhs.n) return true;
        if(n > rhs.n) return false;
        rFOR(i,n-1,0) { if(rhs.a[i] > a[i]) return true; if(a[i] > rhs.a[i]) return false;}
        return true;
    }
    Bignum operator - (const Bignum& rhs) const
    {
        Bignum ret ; ret.n = n;
        int mu = 0;
        rep(i,0,n)
        {
            int tmp;
            if(i < rhs.n) tmp = a[i] - rhs.a[i] - mu;
            else tmp = a[i] - mu;
            if(tmp < 0)
            {
                mu = 1;
                tmp += 2;
            }
            else mu = 0;
            ret.a[i] = tmp;
        }
        while(ret.n > 0 && ret.a[ret.n-1] == 0) ret.n--;
        return ret;
    }
    Bignum Div2()
    {
        Bignum ret ; ret.n = n - 1;
        rep(i,0,n-1) ret.a[i] = a[i+1];
        return ret;
    }
};
void gcd(Bignum a , Bignum b)
{
    int zero = 0;
    while(a.n && b.n)
    {
        if(a.a[0])
        {
            if(b.a[0])
            {
                if(b < a) a = a - b;
                else b = b - a;
            }
            else b = b.Div2();
        }
        else
        {
            if(b.a[0]) a = a.Div2();
            else
            {
                a = a.Div2();
                b = b.Div2();
                zero++;
            }
        }
    }
    if(a.n) a.output();
    else b.output();
    while(zero--)printf("0");
    printf("\n");
}
char s[maxn];
Bignum a , b;
int main()
{
    int T , kcase = 1;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%s",s);
        a.input(s);
        scanf("%s",s);
        b.input(s);
        printf("Case #%d: ",kcase++);
        gcd(a , b);
    }
    return 0;
}

JAVA 版:
import java.math.BigInteger;
import java.util.*;
import java.io.*;

public class Main
{
    public static void main(String args[])
    {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();        
        for(int i = 1; i <= n; ++i)
        {
            BigInteger a = in.nextBigInteger(2);
            BigInteger b = in.nextBigInteger(2);
            BigInteger ans = a.gcd(b);
            System.out.println("Case #" + i + ": "+ans.toString(2));
        }
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值