Java_大数

最近学了一点Java,顺便把一些基本的大数类题目做了。


我的第一个java程序 ---- A+B

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1000

程序如下:

import java.util.Scanner;

public class Main {

    /**
     * @param args
     */
    public static void main(String[] args) 
    {
        // TODO Auto-generated method stub
        Scanner scan = new Scanner(System.in);
        while(scan.hasNext())
        {
            int a = scan.nextInt();
            int b = scan.nextInt();
            System.out.println(a+b);
        }
        //scan.close();
    }

}

求n的阶乘

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1042

程序如下:

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

public class Main {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner cin = new Scanner(System.in);
        while(cin.hasNext())
        {
            int n = cin.nextInt();
            BigInteger fact = BigInteger.ONE;
            for(int i=1; i<=n; i++)
                fact = fact.multiply(BigInteger.valueOf(i));
            System.out.println(fact);
        }
    }

}

数字比较

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2054

程序如下:

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

public class Main {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner cin = new Scanner(System.in);
        while(cin.hasNext())
        {
            BigDecimal a = cin.nextBigDecimal();
            BigDecimal b = cin.nextBigDecimal();
            int flag = a.compareTo(b);
            if(flag == 0) System.out.println("YES");
            else System.out.println("NO");
        }
    }

}

递推运算

题目链接:http://oj.jxust.cn/problem.php?id=1167

程序如下:

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

public class Main {

	/**
	 * @param args
	 */
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		BigInteger f[] = new BigInteger[10000];
		f[1] = f[2] = BigInteger.ONE;
        Scanner cin = new Scanner(System.in);
        while(cin.hasNext())
        {
        	int n = cin.nextInt();
            for(int i=3; i<=n; i++)
            	f[i] = f[i-1].add(f[i-2]);
            System.out.println(f[n]);
        }
	}

}


你可能感兴趣的:(java,大数)