大数问题(Java BigInteger类)

配置好了Java开发环境。

练习使用BigInteger类。


需要注意的一点是:杭电OJ提交Java代码时,类名必须是 Main。


hduoj_1002

简单题。

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

public class hduoj_1002 {
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        int c =  scanner.nextInt();

        BigInteger a,b;

        int i;
        for(i=1;i<=c;i++)
        {
            a = scanner.nextBigInteger();
            b = scanner.nextBigInteger();

            System.out.println("Case " + i + ":");
            System.out.println(a + " + " + b + " = " + a.add(b));

            if(i<c)
                System.out.println();
        }
    }
}


hduoj_1250

附代码。

运行的时候需要将类名改为Main。

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

public class hduoj_1250 {
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);

        int n;

        BigInteger a,b,c,d,e;
;
        while(scanner.hasNext()){
            n = scanner.nextInt();

            if(n<=4)
            {
                System.out.println(1);
                continue;
            }

            a = new BigInteger("1");
            e = b = c = d = a;

            n = n-4;
            while(n-- > 0){
                e = a.add(b.add(c.add(d)));
                a = b;
                b = c;
                c = d;
                d = e;
            }
            System.out.println(d);
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_32862515/article/details/80790022