항 저 우 전기 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
모든 행동 에 대응 하 는 f(Pi)를 N 줄 로 출력 합 니 다.
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); 
        } 
    } 
} 

좋은 웹페이지 즐겨찾기