Java大数类题型训练

Fibonacci Numbers
Time Limit: 2 Seconds Memory Limit: 65536 KB
A Fibonacci sequence is calculated by adding the previous two members of the sequence, with the first two members being both 1.
f(1) = 1, f(2) = 1, f(n > 2) = f(n - 1) + f(n - 2)

Your task is to take a number as input, and print that Fibonacci number.

Sample Input

100

Sample Output

354224848179261915075

Note:

No generated Fibonacci number in excess of 1000 digits will be in the test data, i.e. f(20) = 6765 has 4 digits.
这是第一题,真是恶心,到头来是少了while(cin.hasNext())
需要导的包看来就这两个util.* 和math.*

import java.util.*;
import java.math.*;
public class Main{
    public static void main(String args[]){
        Scanner cin = new Scanner(System.in);
            while(cin.hasNext()){
            int n = cin.nextInt();
            if(n==1) System.out.println(n);
            else {
                BigInteger f[]=new BigInteger[n];
                f[0]=f[1]=new BigInteger("1");
                for(int i=2;i<n;i++) f[i]=f[i-1].add(f[i-2]);

                System.out.println(f[n-1]);
            }
            }


    }
}

A + B Problem II

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 276033 Accepted Submission(s): 53273

Problem Description
I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.

Input
The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000.

Output
For each test case, you should output two lines. The first line is “Case #:”, # means the number of the test case. The second line is the an equation “A + B = Sum”, Sum means the result of A + B. Note there are some spaces int the equation. Output a blank line between two test cases.

Sample Input
2
1 2
112233445566778899 998877665544332211

Sample Output
Case 1:
1 + 2 = 3

Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110

hdu上的几道大数,还PE了,回车处理的问题,所以哪一道题都不能粗心大意,要像做比赛一样平时练题。

import java.util.*;
import java.math.*;
public class Main{
    public static void main(String args[]){
        Scanner cin = new Scanner(System.in);
        int t=cin.nextInt();int kase=1;
        for(int i=1;i<=t;i++) {
            System.out.println("Case "+ (i) + ":");
            BigInteger a=cin.nextBigInteger(),b=cin.nextBigInteger();

            System.out.println(a+" + "+b+" = "+a.add(b));
            if(i!=t) System.out.println("");
        }

    }
}

N!

Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 67586 Accepted Submission(s): 19372

Problem Description
Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N!

Input
One N in one line, process to the end of file.

Output
For each N, output N! in one line.

Sample Input
1
2
3

Sample Output
1
2
6

用到了把int强制转换成BigInteger
先把int变成string string.ValueOf(it)
然后new一个BigInteger

import java.util.*;
import java.math.*;
public class Main{
    public static void main(String args[]){
        Scanner cin = new Scanner(System.in);
       while(cin.hasNext()){
        int n=cin.nextInt();
        BigInteger ans =new BigInteger("1");
        for(int i=1;i<=n;i++){
            ans = ans.multiply(new BigInteger(String.valueOf(i)));
        }
        System.out.println(ans);
       }
    }
}

Integer Inquiry

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 16021 Accepted Submission(s): 4134

Problem Description
One of the first users of BIT’s new supercomputer was Chip Diller. He extended his exploration of powers of 3 to go from 0 to 333 and he explored taking various sums of those numbers.
This supercomputer is great,'' remarked Chip.I only wish Timothy were here to see these results.” (Chip moved to a new apartment, once one became available on the third floor of the Lemon Sky apartments on Third Street.)

Input
The input will consist of at most 100 lines of text, each of which contains a single VeryLongInteger. Each VeryLongInteger will be 100 or fewer characters in length, and will only contain digits (no VeryLongInteger will be negative).

The final input line will contain a single zero on a line by itself.

Output
Your program should output the sum of the VeryLongIntegers given in the input.

This problem contains multiple test cases!

The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.

Sample Input
1

123456789012345678901234567890
123456789012345678901234567890
123456789012345678901234567890
0

Sample Output
370370367037037036703703703670

import java.util.*;
import java.math.*;
public class Main{
    public static void main(String args[]){
        Scanner cin = new Scanner(System.in);
        int t=cin.nextInt();
        for(int i=0;i<t;i++){
            BigInteger sum = new BigInteger("0");
            BigInteger p = new BigInteger("0");
            while(true){
            BigInteger n=cin.nextBigInteger();
            if(n.equals(p)) break;
            sum = sum.add(n);

            }
           System.out.println(sum);
           if(i!=t-1) System.out.println("");
        }

    }
}

 Try your best

Try your best

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值