ACM之java

输入,格式输出

代码1:

import java.math.*;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner cin=new Scanner (System.in);
        BigInteger a,b,temp;
        int i,j,n,m,t;
        t=cin.nextInt();
        for(i=1;i<=t;++i){
            n=cin.nextInt();
            m=cin.nextInt();
            if(n<m){
                System.out.println("0");
                continue;
            }
            a=BigInteger.valueOf(1);
            for(j=n-m+1;j<=n;++j){
                temp=BigInteger.valueOf(j);
                a=a.multiply(temp);      
            }
            b=BigInteger.valueOf(1);
            for(j=1;j<=m;++j){
                temp=BigInteger.valueOf(j);
                b=b.multiply(temp);
            }
            a=a.divide(b);
            System.out.println(a);     
        }

       
    }

}

 代码2:

import java.math.*; //大数的包
import java.text.*; //格式的包
import java.util.Scanner; //输入的包

public class Main {
    public static void main(String[] args) {
        Scanner cin=new Scanner(System.in);
        DecimalFormat f=new DecimalFormat("#.####");
        BigDecimal a,b;
        while(cin.hasNext() ){
           a=cin.nextBigDecimal();
           b=cin.nextBigDecimal();
           a=a.add(b);
           System.out.println(f.format(a));
          // System.out.println(a);
        }
    }

}

比较两个大数的大小:if(a>b) 即 a.compareTo(b)>0

3.大数操作

题目:

多少 Fibs

Time Limit:1000MS  Memory Limit:65536K
Total Submit:28 Accepted:20 

Description 

重新定义Fibonacci数: 
F1=1 
F2=2 
Fn=fn-1+fn-2 
现给出a和b,计算一下[a,b]间有多少Fibonacci数; 


Input 

多CASE,每次测试包含两个数,a,b,a<=b<=10^100, 
a=b=0时,输入结束。 


Output 

每个case一个数,fibs的数目

Sample Input 


10 100
1234567890 9876543210
0 0

Sample Output 


5
4


Source 

LBB

 代码:

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

public class Main{
	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		int now;
		while( cin.hasNext()) {
			BigInteger a, b, sum;
			a = cin.nextBigInteger();
			b = cin.nextBigInteger();
			if( a.equals(BigInteger.valueOf(0)) && b.equals(BigInteger.valueOf(0))) break;
			BigInteger num[] = new BigInteger[3];
			num[0] = BigInteger.valueOf(0);
			num[1] = BigInteger.valueOf(1);
			now = 2;
			num[now] = num[(now+1)%3].add(num[(now+2)%3]);
			while( a.compareTo(num[now]) > 0 ) {
				now = (now+1) % 3; 
				num[now] = num[(now+1)%3].add(num[(now+2)%3]);
			}
			sum = BigInteger.valueOf(0);
			while( b.compareTo(num[now]) >= 0 ) {
				sum = sum.add(BigInteger.valueOf(1));
				now = (now+1) % 3;
				num[now] = num[(now+1)%3].add(num[(now+2)%3]);	
			}
			System.out.println(sum);
		}
	}
}

函数调用:

//hdoj 4523 威威猫系列故事——过生日
import java.math.*;
import java.util.Scanner;
 
public class Main {
	public static boolean solve(BigInteger n, BigInteger m, BigInteger p) {
		BigInteger three = BigInteger.valueOf(3);
		BigInteger zero = BigInteger.valueOf(0);
		if(m.compareTo(three) < 0 || (n.add(p)).compareTo(m) < 0 || (p.compareTo(zero) == 0 && n.compareTo(m) != 0)) 
			return false;
		return true;
	}
	
    public static void main(String[] args) {
        Scanner cin=new Scanner (System.in);
        BigInteger m, n, p;
        while(cin.hasNext()) {
        	n = cin.nextBigInteger();
        	m = cin.nextBigInteger();
        	p = cin.nextBigInteger();
        	if(solve(n, m, p)) System.out.println("YES");
        	else System.out.println("NO");
        } 
    }
}

 。

 

posted on 2011-03-08 15:37  CrazyAC  阅读(518)  评论(0编辑  收藏  举报