美文网首页ACM题库~
杭电oj-1002(A + B Problem II)

杭电oj-1002(A + B Problem II)

作者: 关玮琳linSir | 来源:发表于2016-08-08 21:14 被阅读153次

    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
    

    Author

    Ignatius.L
    

    这道题考我们的是大数计算问题,也就是说一个超过我们系统中long类型的数,计算加法的时候,需要我们自己处理这里面的逻辑,但是无奈,java为我们想好了这一切,我们java中内置了一个java.math.BigInteger工具类,可以直接计算大数,所有的问题都解决咯。

    顺便说一下,本人喜欢java的原因,可能也就是在最开始学编程的时候,没怎么见过像eclipse一样的编译器,后来就喜欢上了它,再后来自己就在学习安卓啦,对java的语法也比较喜欢,就这样咯,换句话说,笔者的c学的不好,而acm也不支持什么别的语言啦,那就java吧,但是有的时候java确实会解题时间长的问题,但是这种情况非常的少,目前已知的很多acm大神也在使用java的~

    代码:

    
    import java.math.BigInteger;
    import java.util.Scanner;
    
    public class Main1002 {
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            int n = in.nextInt();
            for (int i = 0; i < n; i++) {
                BigInteger a = in.nextBigInteger();
                BigInteger b = in.nextBigInteger();
                
                if (i == n - 1)// 输出格式控制
                {
                    System.out.println("Case " + (i + 1) + ":\r\n" + a + 
                    " + " + b + " = " + a.add(b));
                } else {
                    System.out.println("Case " + (i + 1) + ":\r\n" + a + 
                    " + " + b + " = " + a.add(b) + "\r\n");
                }
            }
        }
    }
    

    相关文章

      网友评论

        本文标题:杭电oj-1002(A + B Problem II)

        本文链接:https://www.haomeiwen.com/subject/xlvrsttx.html