码迷,mamicode.com
首页 > 编程语言 > 详细

HDU中大数实现的题目,持续更新(JAVA实现)

时间:2015-03-14 21:22:07      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:

HDU1002:大数加法,PE了N次

技术分享
import java.util.Scanner;
import java.math.*;

public class Main
{
    public static void main(String[] args)
    {
        Scanner cin = new Scanner(System.in);
        int T;
        T=cin.nextInt();
        for(int z=1;z<=T;z++)
        {
            if(z!=1) System.out.println();
            BigInteger a,b;
            a=cin.nextBigInteger();
            b=cin.nextBigInteger();
            System.out.println("Case "+z+":");
            System.out.println(a+" + "+b+" = "+a.add(b));
            
        }
        cin.close();
    }

}
View Code

HDU1042:N!大数乘法

技术分享
import java.util.Scanner;
import java.math.*;

import javax.naming.ldap.HasControls;


public class Main{

    public static void main(String[] args)
    {
        Scanner cin = new Scanner(System.in);
        int t;
        while(cin.hasNext())
        {
            BigInteger n=BigInteger.valueOf(1);
            t=cin.nextInt();
            for(int i=2;i<=t;i++)
            {
                n=n.multiply(BigInteger.valueOf(i));
            }
            System.out.println(n);
        }
        
    }
        
}
View Code

HDU1316:How Many Fibs?

import java.math.*;
import java.util.Scanner;
public class Main 
{
    public static void main(String[] args)  
    {
        Scanner cin = new Scanner(System.in);
        BigInteger []f=new BigInteger[1020];
        f[0]=BigInteger.valueOf(1);
        f[1]=BigInteger.valueOf(1);
        f[2]=BigInteger.valueOf(2);
        for(int i=3;i<1019;i++)
            f[i]=f[i-1].add(f[i-2]);
        BigInteger a,b;
        int sum;
        while(cin.hasNextBigInteger())
        {
            a=cin.nextBigInteger();
            b=cin.nextBigInteger();
            if(a.compareTo(BigInteger.ZERO)==0&&b.compareTo(BigInteger.ZERO)==0) break;
            sum=0;
            for(int i=1;i<=1015;i++)
            {
               if(f[i].compareTo(a)>=0&&f[i].compareTo(b)<=0)
               {
                  sum++;
               }
               if(f[i].compareTo(b)>0) break;
            }
            System.out.println(sum);
        }
        cin.close();
    }

}

HDU1753: 大明 A+B 大数加法加一些函数

import java.math.*;
import java.util.Scanner;
public class Main 
{
    public static void main(String[] args)  
    {
        Scanner cin = new Scanner(System.in);
        BigDecimal a,b;
        while(cin.hasNextBigDecimal())
        {
            a=cin.nextBigDecimal();
            b=cin.nextBigDecimal();
            System.out.println(a.add(b).stripTrailingZeros().toPlainString());
        }
        cin.close();
    }

}

 

HDU中大数实现的题目,持续更新(JAVA实现)

标签:

原文地址:http://www.cnblogs.com/zhangmingcheng/p/4338139.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!