Java中大数初级使用

 

http://acm.hdu.edu.cn/showproblem.php?pid=1002

大数相加

 

package com.hdu;

import java.math.BigInteger;
import java.util.Scanner;

public class BigNum {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int t = in.nextInt();
		BigInteger c;
		//System.out.println(t);
		for(int i=1;i<=t;i++){		
				BigInteger a = new BigInteger(in.next());			
				BigInteger b = new BigInteger(in.next());	
				c = a.add(b) ;
				System.out.println("Case "+i+":"); //不知道为啥用printf 就PE
				System.out.print(a+" + "+b+" = ");
				System.out.println(c);      
				if(i<t) System.out.println();
		}	
	}
}

/*
 * 2 
 * 1 2 
 * 112233445566778899 998877665544332211
 */


 

 

 

 

http://acm.hdu.edu.cn/showproblem.php?pid=2054

 

判断两个大数是否相等

package com.hdu;

import java.math.BigDecimal;
import java.util.Scanner;

public class AisEuqalB {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		while(in.hasNext()){
			BigDecimal a = in.nextBigDecimal();
			BigDecimal b = in.nextBigDecimal();
			if(a.compareTo(b)==0) System.out.println("Yes");
			else System.out.println("NO");
		}
	}
}


 

 

 

你可能感兴趣的:(Java中大数初级使用)