HDU1002 A + B Problem II 高精度

HDU1002 A + B Problem II

/*Java 大整数和
 *405MS	11748K
 */
import java.math.BigInteger;  
import java.util.Scanner; 

public class Main
{
	public static void main (String[] args)
	{
		Scanner in = new Scanner(System.in);
		int T=in.nextInt();
		for (int k=1;k<=T;k++)
		{
			BigInteger a=in.nextBigInteger();
			BigInteger b=in.nextBigInteger();
			System.out.println("Case "+k+":");
			System.out.println(a+" + "+b+" = "+a.add(b));
			if (k!=T)
				System.out.println();
		}
	}
}
/* C++ 大整数和
0MS	1720K
*/
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <string>
using namespace std;
struct BigInt
{
    const static int mod=10000;
    const static int DLEN=4;
    int a[600],len;
    BigInt()
    {
        memset(a,0,sizeof (a));
        len=1;
    }
    BigInt(int v)
    {
        memset (a,0,sizeof (a));
        len=0;
        do
        {
            a[len++]=v%mod;
            v/=mod;
        }while (v);
    }
    BigInt(const string s)
    {
        memset (a,0,sizeof (a));
        int L=s.length();
        len=L/DLEN;
        if (L%DLEN) len++;
        int index=0;
        for (int i=L-1;i>=0;i-=DLEN)
        {
            int t=0;
            int k=i-DLEN+1;
            if (k<0) k=0;
            for (int j=k;j<=i;j++)
                t=t*10+s[j]-'0';
            a[index++]=t;
        }
    }
    BigInt (const BigInt &T):len(T.len)
    {
        memset (a,0,sizeof (a));
        for (int i=0;i<len;i++)
            a[i]=T.a[i];
    }
    BigInt operator +(const BigInt &b) const
    {
        BigInt res;
        res.len=max(len,b.len);
        for (int i=0;i<=res.len;i++)
            res.a[i]=0;
        for (int i=0;i<res.len;i++)
        {
            res.a[i]+=((i<len)?a[i]:0)+((i<b.len)?b.a[i]:0);
            res.a[i+1]+=res.a[i]/mod;
            res.a[i]%=mod;
        }
        if (res.a[res.len]>0) res.len++;
        return res;
    }
    void output()
    {
        printf ("%d",a[len-1]);
        for (int i=len-2;i>=0;i--)
            printf ("%04d",a[i]);
        printf ("\n");
    }
};
int main ()
{
    string a,b;
    int T;
    while (cin>>T!=0)
    {
        for (int k=1;k<=T;k++)
        {
            cin>>a>>b;
            BigInt a1(a);
            BigInt b1(b);
            a1=a1+b1;
            cout<<"Case "<<k<<":"<<endl;
            cout<<a<<" + "<<b<<" = ";
            a1.output();
            if (k!=T) cout<<endl;
        }
    }
    return 0;
}

高精度模板:

/*c++大整数模板 高效
*/
#include <bits/stdc++.h>
using namespace std;
struct BigInt
{
    const static int mod=10000;
    const static int DLEN=4;
    int a[600],len;
    BigInt()
    {
        memset(a,0,sizeof (a));
        len=1;
    }
    BigInt(int v)
    {
        memset (a,0,sizeof (a));
        len=0;
        do
        {
            a[len++]=v%mod;
            v/=mod;
        }while (v);
    }
    BigInt(const string s)
    {
        memset (a,0,sizeof (a));
        int L=s.length();
        len=L/DLEN;
        if (L%DLEN) len++;
        int index=0;
        for (int i=L-1;i>=0;i-=DLEN)
        {
            int t=0;
            int k=i-DLEN+1;
            if (k<0) k=0;
            for (int j=k;j<=i;j++)
                t=t*10+s[j]-'0';
            a[index++]=t;
        }
    }
    BigInt (const BigInt &T):len(T.len)
    {
        memset (a,0,sizeof (a));
        for (int i=0;i<len;i++)
            a[i]=T.a[i];
    }
    BigInt operator +(const BigInt &b) const
    {
        BigInt res;
        res.len=max(len,b.len);
        for (int i=0;i<=res.len;i++)
            res.a[i]=0;
        for (int i=0;i<res.len;i++)
        {
            res.a[i]+=((i<len)?a[i]:0)+((i<b.len)?b.a[i]:0);
            res.a[i+1]+=res.a[i]/mod;
            res.a[i]%=mod;
        }
        if (res.a[res.len]>0) res.len++;
        return res;
    }
    BigInt operator -(const BigInt &T) const
    {
        int i,j,big;
        bool flag;
        BigInt t1,t2;
        if (*this >T)
        {
            t1=*this;
            t2=T;
            flag=0;
        }
        else
        {
            t1=T;
            t2=*this;
            flag=1;
        }
        big=t1.len;
        for (i=0;i<big;i++)
        {
            if (t1.a[i]<t2.a[i])
            {
                j=i+1;
                while (t1.a[j]==0)
                    j++;
                t1.a[j--]--;
                while (j>i)
                    t1.a[j--]+=mod-1;
                t1.a[i]+=mod-t2.a[i];
            }
            else t1.a[i]-=t2.a[i];
        }
        t1.len=big;
        while (t1.a[len-1]==0&&t1.len>1)
        {
            t1.len--;
            big--;
        }
        if (flag)
            t1.a[big-1]=0-t1.a[big-1];
        return t1;
    }
    BigInt operator *(const BigInt &b) const
    {
        BigInt res;
        for (int i=0;i<len;i++)
        {
            int up=0;
            for (int j=0;j<b.len;j++)
            {
                int temp=a[i]*b.a[j]+res.a[i+j]+up;
                res.a[i+j]=temp%mod;
                up=temp/mod;
            }
            if (up!=0)
                res.a[i+b.len]=up;
        }
        res.len=len+b.len;
        while (res.a[res.len-1]==0&&res.len>1) res.len--;
        return res;
    }
    BigInt operator /(const int &b) const//sub int
    {
        BigInt res;
        int i,down=0;
        for (i=len-1;i>=0;i--)
        {
            res.a[i]=(a[i]+down*mod)/b;
            down=a[i]+down*mod-res.a[i]*b;
        }
        res.len=len;
        while (res.a[res.len-1]==0&&res.len>1)
            res.len--;
        return res;
    }
    BigInt operator %(const int &b) const//mod int
    {
        int i,d=0;
        for (i=len-1;i>=0;i--)
            d=((d*mod)%b+a[i]%b);
        return d;
    }
    BigInt operator ^(const int &n) const//n fang
    {
        BigInt t,res(1);
        int i;
        int m=n;
        while (m>1)
        {
            t=*this;
            for (i=1;(i<<1)<=m;i<<=1)
                t=t*t;
            m-=i;
            res=res*t;
            if (m==1) res=res*(*this);
        }
        return res;
    }
    bool operator >(const BigInt &T) const//dayv 
    {
        int In;
        if (len>T.len) return true;
        else if (len==T.len)
        {
            In=len-1;
            while (a[In]==T.a[In]&&In>=0)
                In--;
            if (In>=0&&a[In]>T.a[In])
                return true;
            else return false;
        }
        else return false;
    }
    bool operator >(const int &t) const
    {
        BigInt b(t);
        return *this>b;
    }
    void output()
    {
        printf ("%d",a[len-1]);
        for (int i=len-2;i>=0;i--)
            printf ("%04d",a[i]);
        printf ("\n");
    }
};
/*Java 大整数 方便
*/
import java.math.BigInteger;  
import java.util.Scanner; 

public class Main
{
	public static void main (String[] args)
	{
		Scanner in = new Scanner(System.in);
		BigInteger a=in.nextBigInteger();
		BigInteger b=in.nextBigInteger();
		BigInteger c;
		c=a.add(b);//+
		c=a.subtract(b);//-
		c=a.multiply(b);//*
		c=a.divide(b);///
		c=a.mod(b);//%
		int cmp=a.compareTo(b);//=(0) <(-x) >(x)
	}
}

/*Java 大实数数 方便
*/
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Scanner; 

public class Main
{
	public static void main (String[] args)
	{
		Scanner in = new Scanner(System.in);
		BigDecimal a=in.nextBigDecimal();
		BigDecimal b=in.nextBigDecimal();
		BigDecimal c;
		c=a.add(b);//+
		c=a.subtract(b);//-
		c=a.multiply(b);//*
		c=a.divide(b,10,RoundingMode.HALF_UP);/// 四舍五入 保留10位小数 可不加后两项
		int cmp=a.compareTo(b);//=(0) <(-x) >(x)
		System.out.println(c);
	}
}



猜你喜欢

转载自blog.csdn.net/nrtostp/article/details/80154651
今日推荐