杭电ACM大数JAVA提交实例

(提交时需将类名改为Main)

1002A + B Problem II

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

import java.math.BigInteger; 
import java.util.Scanner; 
  
public class Main{ 
    public static void main(String args[]){ 
        BigInteger a = BigInteger.valueOf(0),b = BigInteger.valueOf(0); 
        int c; 
        Scanner cin = new Scanner(System.in); 
        boolean flag = false; 
        while(cin.hasNextInt()){ 
            c = cin.nextInt(); 
            for(int i = 1;i<=c;i++){ 
                a = cin.nextBigInteger(); 
                b = cin.nextBigInteger(); 
                if(flag){ 
                    System.out.println(); 
                } 
                flag = true; 
                System.out.println("Case "+i+":"); 
                System.out.println(a+" + "+b+" = "+a.add(b)); 
            } 
        } 
    } 
} 

 

1042N!

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

import java.math.BigInteger;

import java.util.Scanner;

 

public class Main{

    public static void main(String[] args) {

        int n;

        BigInteger mul;

        Scanner cin = new Scanner(System.in);

        while (cin.hasNextInt()) {

            n = cin.nextInt();

            mul = BigInteger.ONE;

            for (int i = 1; i <= n; i++) {

                mul = mul.multiply(BigInteger.valueOf((long) i));

            }

            System.out.println(mul);

        }

    }

}

 

1047Integer Inquiry

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.math.BigInteger; 
import java.util.Scanner; 
  
public class Main{ 
    public static void main(String[] args) { 
        int n; 
        BigInteger tmp, res; 
        boolean flag = false; 
        Scanner cin = new Scanner(System.in); 
        cin.hasNextInt(); 
        n = cin.nextInt(); 
        for (int i = 0; i < n; i++) { 
            if (flag) 
                System.out.println(); 
            flag = true; 
            res = BigInteger.ZERO; 
            while (cin.hasNextBigInteger()) { 
                tmp = cin.nextBigInteger(); 
                if (tmp.intValue() == 0) 
                    break; 
                res = res.add(tmp); 
            } 
            System.out.println(res); 
        } 
    } 
} 

1063Exponentiation

Problem Description

Problems involving the computation of exact values of very large magnitude and precision are common. For example, the computation of the national debt is a taxing experience for many computer systems.
This problem requires that you write a program to compute the exact value of Rn where R is a real number ( 0.0 < R < 99.999 ) and n is an integer such that 0 < n <= 25.

Input

The input will consist of a set of pairs of values for R and n. The R value will occupy columns 1 through 6, and the n value will be in columns 8 and 9.

Output

The output will consist of one line for each line of input giving the exact value of R^n. Leading zeros should be suppressed in the output. Insignificant trailing zeros must not be printed. Don't print the decimal point if the result is an integer.

Sample Input

95.123 12

0.4321 20

5.1234 15

6.7592  9

98.999 10

1.0100 12

Sample Output

548815620517731830194541.899025343415715973535967221869852721

.00000005148554641076956121994511276767154838481760200726351203835429763013462401

43992025569.928573701266488041146654993318703707511666295476720493953024

29448126.764121021618164430206909037173276672

90429072743629540498.107596019456651774561044010001

1.126825030131969720661201

 

import java.math.BigDecimal;

import java.util.Scanner;

public class Main{

    public static void main(String args[]) {

        BigDecimal r, mul;

        int n;

        Scanner cin = new Scanner(System.in);

        while (cin.hasNextBigDecimal()) {

            r = cin.nextBigDecimal();

            mul = BigDecimal.ONE;

            n = cin.nextInt();

            for (int i = 0; i < n; i++) {

                mul = mul.multiply(r);

            }

            String str = mul.stripTrailingZeros().toPlainString();

            if (str.charAt(0) == '0')

                System.out.println(str.substring(1));

            else

                System.out.println(str);

        }

    }

}

 

1316How Many Fibs?

Problem Description

Recall the definition of the Fibonacci numbers:
f1 := 1
f2 := 2
fn := fn-1 + fn-2 (n >= 3)
Given two numbers a and b, calculate how many Fibonacci numbers are in the range [a, b].

Input

The input contains several test cases. Each test case consists of two non-negative integer numbers a and b. Input is terminated by a = b = 0. Otherwise, a <= b <= 10^100. The numbers a and b are given with no superfluous leading zeros.

Output

For each test case output on a single line the number of Fibonacci numbers fi with a <= fi <= b.

Sample Input

10 100

1234567890 9876543210

0 0

Sample Output

5

4

import java.math.BigInteger; 
import java.util.Scanner; 
  
public class Main{ 
    public static void main(String args[]) { 
        BigInteger count; 
        BigInteger f[] = new BigInteger[10005]; 
        BigInteger a = BigInteger.valueOf(0), b = BigInteger.valueOf(0); 
        f[1] = BigInteger.valueOf(1); 
        f[2] = BigInteger.valueOf(2); 
        for (int i = 3; i < 10005; i++) { 
            f[i] = f[i - 1].add(f[i - 2]); 
        } 
        Scanner cin = new Scanner(System.in); 
        while (cin.hasNextBigInteger()) { 
            a = cin.nextBigInteger(); 
            b = cin.nextBigInteger(); 
            if (a.equals(BigInteger.valueOf(0)) 
                    && b.equals(BigInteger.valueOf(0))) { 
                break; 
            } else { 
                count = BigInteger.valueOf(0); 
                for (int i = 1; i < 10000; i++) { 
                    if (((a.compareTo(f[i]) == -1) || (a.compareTo(f[i]) == 0)) 
                            && ((b.compareTo(f[i]) == 1) || (b.compareTo(f[i]) == 0))) { 
                        count = count.add(BigInteger.valueOf(1)); 
                    } 
                } 
                System.out.println(count); 
            } 
        } 
    } 
} 

1715大菲波数

Problem Description

Fibonacci数列,定义如下:
f(1)=f(2)=1
f(n)=f(n-1)+f(n-2) n>=3。
计算第n项Fibonacci数值。

Input

输入第一行为一个整数N,接下来N行为整数Pi(1<=Pi<=1000)。

Output

输出为N行,每行为对应的f(Pi)。

Sample Input

5

1

2

3

4

5

Sample Output

1

1

2

3

5

import java.io.*; 
import java.util.*; 
import java.math.*; 
public class Main{ 
    public static void main(String[] args) { 
        int n; 
        BigInteger f[] = new BigInteger[1005]; 
        f[1] = BigInteger.valueOf(1); 
        f[2] = BigInteger.valueOf(1); 
        for (int i = 3; i < 1005; i++) { 
            f[i] = f[i - 1].add(f[i - 2]); 
        } 
        Scanner cin = new Scanner(System.in); 
        while (cin.hasNextInt()) { 
            n = cin.nextInt(); 
            int p; 
            for (int i = 0; i < n; i++) { 
                p = cin.nextInt(); 
                System.out.println(f[p]); 
            } 
        } 
    } 
} 

1753大明A+B

Problem Description

话说,经过了漫长的一个多月,小明已经成长了许多,所以他改了一个名字叫“大明”。
这时他已经不是那个只会做100以内加法的那个“小明”了,现在他甚至会任意长度的正小数的加法。
现在,给你两个正的小数A和B,你的任务是代表大明计算出A+B的值。

Input

本题目包含多组测试数据,请处理到文件结束。
每一组测试数据在一行里面包含两个长度不大于400的正小数A和B。

Output

请在一行里面输出输出A+B的值,请输出最简形式。详细要求请见Sample Output。

Sample Input

1.1 2.9

1.1111111111 2.3444323343

1 1.1

Sample Output

4

3.4555434454

2.1

import java.io.*; 
import java.util.*; 
import java.math.*; 
  
public class Main { 
    public static void main(String[] args) { 
        BigDecimal a, b; 
        Scanner cin = new Scanner(System.in); 
        while (cin.hasNextBigDecimal()) { 
            a = cin.nextBigDecimal(); 
            b = cin.nextBigDecimal(); 
            a = a.add(b); 
            String str = a.stripTrailingZeros().toPlainString(); 
            System.out.println(str); 
        } 
    } 
} 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值