1. 程式人生 > >杭電ACM大數JAVA提交例項

杭電ACM大數JAVA提交例項

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

相關推薦

ACM大數JAVA提交例項

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

ACM oj 1002 大數相加,求助

杭電oj 1002題大數相加,格式與AC的相同,試了很多結果都是對的,但就是過不了,有大神幫忙看一下好嗎?謝謝! #include<stdio.h> #include<string.h> int main(void) { char a[

acm大數階乘(附原始碼)

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

ACM第1002題(A + B Problem II)答案 java

方法一: package hduacm; import java.util.Scanner; public class Main{ public static void main(String[] args) throws Exception{

ACM——Java

記錄杭電ACM的部分答案,純手寫,如有雷同,算你抄我的。o(∩_∩)o 1001 問題描述 import java.util.*; public class Main {

ACM 1040 As Easy As A+B java 解讀

As Easy As A+B Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 48014    Accepte

2017ACM集訓隊單人排位賽 - 2 題解

需要 6.0 hdu2045 owb style 適應 freopen count 枚舉   1001,水題,直接模擬即可。比賽中開局連wa三發,因為把int寫成了bool..   1002,積分題,比賽中找到了下面這個積分公式,   但是並沒什麽用,,因為帶入以後存在誤

1047——大數相加

gpo 杭電 tro 格式 oid 註意 大數相加 != body #include <stdio.h> #include <string.h> void add(char d1[],char d2[],char sum[]) { int i

acm2059-龜兔賽跑 java

for system 電量 () 最短 [] 設置 如何 color 一看題就知道是動態規劃,不過這要看下如何設置變化數組了 先分析這道題:兔子到達終點的時間時固定的,因此只需要考慮烏龜了,烏龜騎電車和騎自行車的時間,然後計算,因為中間有N個充電站,可以看做N個點(到起點的

acm 1173

ace 學習 一個點 bsp esp != 所有 fin 數學 從1173這道題,我學習到了一個數學思想...... 假設所有的點都在一個直線上,要求一個點,到所有點的距離之和最短,那麽,把值最大的點和值最小的點連成一條直線,這條直線上的所有點到所有的點的距離之和相等且都是

acm 1180 詭異的樓梯 BFS

script 朋友 mission emp temp ont pty content using 詭異的樓梯 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Other

acm 1181 變形課 DFS

小寫 pty space ring 隊列 set amp ott panel 變形課 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Subm

acm 1210 Eddy's 洗牌問題

input pro sample 就是 pre ble 通過 iss ont Eddy‘s 洗牌問題 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot

acm 1230 火星a+b(進制)

進制數 a+b 第一個 進行 () 地球 iss class font 火星A+B Time Limit: 2000/1000 MS (Java/

ACM hdu 2079 選課時間 (模板)

Problem Description 又到了選課的時間了,xhd看著選課表發呆,為了想讓下一學期好過點,他想知道學n個學分共有多少組合。你來幫幫他吧。(xhd認為一樣學分的課沒區別) Input輸入資料的第一行是一個數據T,表示有T組資料。每組資料的第一行是兩個整數n(1 <= n <

acm 1231 最大連續子序列

                          

acm 1230 火星a+b(進位制)

                          

acm 1210 Eddy's 洗牌問題

Eddy's 洗牌問題 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5504  &n

ACM-2602 Bone Collector

                                          &nb

ACM-2075 A|B?

A|B?                                         &nbs