杭电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。

Input

Output

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

Input

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);
       }
   }
}

大数相加的原理及代码(杭电OJ上AC过的)实现

2014-05-20 23:28:14

杭电 OJ 提交代码需要注意的问题

2015-03-23 11:29:14

HDU 1042 N! (大数阶乘,紫书上的方法超时!!还是Java大法好!!)

2016-05-12 23:00:04

杭电ACM 三 大数取模

2015-02-20 12:40:01

杭电ACM 1005：Number Sequence

2015-05-15 10:08:03

杭电ACM 1002大数相加 两种方法

2016-07-30 18:40:54

杭电网 提交的 题 总是 Compilation Error

2012-09-21 12:47:30

开始做杭电acm时注意事项

2015-11-21 11:57:46

ACM基础——OJ上的Java代码提交规范

2015-07-24 16:24:17

杭电 HDU 1108 最小公倍数

2015-03-28 21:15:08