int_加法计算器2

[问题2] 加法计算器2

实现四则运算计算器里加法的功能.

 

给两个数 A, B 输出A加B的结果.

 

[限制条件]

1. A, B 是0或者正整数.

2. A, B的位数为30位以下.

 

[输入]

首先给出testcase的个数T,下面每一行给出一个testcase  A和B的值,中间用空格隔开.

 A和B是正整数的情况下,不会以0开始

[输出]

每一行输出一个testcase的结果.以 #x 开始(x 是testcase的号码,从1开始) 空一格后输出结果 正整数的结果不要以0开始.

 

[注意]

不能使用java.math 相关的class(BigInteger, BigDecimal 等). 

[输入例子]

(输入)

5

123 45976

234901 0

2100000000 2100000000

123456789 1111111101

9999999999999999 2

 

(输出)

#1 46099

#2 234901

#3 4200000000

#4 1234567890

#5 10000000000000001

 

(sample_input.txt 의 输出)

#1 0

#2 100000000000000000001

#3 186587978223131665

#4 69000000000000000

#5 12345678912341234567891234568

#6 27945675113615298073416645937

#7 86452149

#8 101010101010101021

#9 1234567890

#10 999999999999999999999999999999

 

 

------------------------

10

0 0

99999999999999999999 2

15298673441329807 171289304781801858

65341867415977986 3658132584022014

12345678912341234567891234567 1

1347864 27945675113615298073415298073

6875429 79576720

11 101010101010101010

246913578 987654312

0 999999999999999999999999999999

--------------------------------

import java.math.BigInteger;

import java.util.*;

 

public class Solution {

        public static void main(String[] args) throws Exception {

                Scanner sc = new Scanner(System.in);

                int t = sc.nextInt();

                

 

                List<BigInteger[]> list = new ArrayList<BigInteger[]>();

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

                        BigInteger[] a = new BigInteger[2];

                        a[0] = new BigInteger(sc.next());

                        a[1] = new BigInteger(sc.next());

                        

                        list.add(a);

                }

                

                for (int i = 0; i < list.size(); i++) {

                        System.out.print("#" + (i + 1));

                        System.out.print(" ");

                        System.out.println(list.get(i)[0].add(list.get(i)[1]));

                }

                

//                BigInteger a = new BigInteger("99999999999999999999");

                

        }

}

-----------------------------

import java.io.*;

import java.util.*;

 

public class Solution {

        

        static int[] A = new int[30];

        static int[] B = new int[30];

        static int sizeA, sizeB; // The number of digits of A, B.

        static int[] Answer = new int[31];

        static int size;

 

        public static void main(String[] args) throws Exception {

                

                String buffer;

                // The method below means that the program will read from input file, 

                // instead of standard(keyboard) input.

                // To test your program, you may save input data in input file,

                // and call below method to read from the file when using nextInt() method.

                // You may remove the comment symbols(//) in the below statement and use it.

                // But before submission, you must remove the Console.SetIn method or rewrite comment symbols(//).

                //System.setIn(new FileInputStream("sample_input.txt"));

 

                // Make new Scanner from standard input System.in, and read data.

                Scanner sc = new Scanner(System.in);

                

                int T = sc.nextInt();

                

                for(int test_case = 1; test_case <= T; ++test_case) {

 

                        // Read A

                        buffer = sc.next();

                        sizeA = buffer.length();

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

                                A[i] = (int)(buffer.charAt(i) - '0');

                        }

                        

                        // Read B

                        buffer = sc.next();

                        sizeB = buffer.length();

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

                                B[i] = (int)(buffer.charAt(i) - '0');

                        }

 

int count=sizeA>sizeB?sizeA:sizeB;

                        int k=0;

                        int sum=0;

                        String result="";

                        size=0;

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

                                int valueA=0;

                                int valueB=0;

                                if(sizeA-i-1>=0){

                                        valueA=A[sizeA-i-1];

                                }

                                

                                if(sizeB-i-1>=0){

                                        valueB=B[sizeB-1-i];

                                }

                                

                                sum = valueA+ valueB + k;

                                if (sum > 9) {

                                        result+= sum % 10;

                                        

                                        k = 1;

                                } else {

                                        result+= sum;

                                        k = 0;

                                }

                                size++;

                        }

                        

                        if(k==1){

                                result+= "1";

                                size++;

                        }

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

                                Answer[i] = (int)(result.charAt(size-1-i) - '0');

                        }                

            

            

                        // Print the answer to standard output(screen).

                        System.out.print("#" + test_case + " ");

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

                                System.out.print(Answer[i]);

                        }

                        System.out.println();

                }

                sc.close();

        }

}

 

---------------------------------

import java.io.*;

import java.util.*;

 

public class Solution {

        

        static int[] A = new int[30];

        static int[] B = new int[30];

        static int sizeA, sizeB; // The number of digits of A, B.

        static int[] Answer = new int[31];

        static int size;

 

        public static void main(String[] args) throws Exception {

                

                String buffer;

                // The method below means that the program will read from input file, 

                // instead of standard(keyboard) input.

                // To test your program, you may save input data in input file,

                // and call below method to read from the file when using nextInt() method.

                // You may remove the comment symbols(//) in the below statement and use it.

                // But before submission, you must remove the Console.SetIn method or rewrite comment symbols(//).

                //System.setIn(new FileInputStream("sample_input.txt"));

 

                // Make new Scanner from standard input System.in, and read data.

                Scanner sc = new Scanner(System.in);

                

                int T = sc.nextInt();

                

                for(int test_case = 1; test_case <= T; ++test_case) {

 

                        // Read A

                        buffer = sc.next();

                        sizeA = buffer.length();

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

                                A[i] = (int)(buffer.charAt(i) - '0');

                        }

                        

                        // Read B

                        buffer = sc.next();

                        sizeB = buffer.length();

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

                                B[i] = (int)(buffer.charAt(i) - '0');

                        }

 

                        //

                        // Implement your algorithm from this section.                //

                        //        

                        if (sizeA < sizeB) {

                                int sizeTemp = sizeA;

                                sizeA = sizeB;

                                sizeB = sizeTemp;

                                

                                int[] temp = A;

                                A=B;

                                B=temp;

                        }

                        

                int restNum = 0;

                size = sizeA;

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

                                int aNum = A[sizeA-1-i];

                                int bNum = i < sizeB ? B[sizeB-1-i]:0;

                                

                                Answer[i] = aNum + bNum + restNum;

                                boolean needAdd = Answer[i] >= 10;

                                

                                restNum = needAdd ? 1:0;

                                if(needAdd){

                                        Answer[i] = Answer[i] - 10;

                                        if(i==sizeA-1){

                                                Answer[i+1] = 1;

                                                size += 1;

                                        }

                                }

                        }

            

                        for (int i = 0; i < size/2; i++) {

                                int temp = Answer[i];

                                Answer[i] = Answer[size-1-i];

                                Answer[size-1-i] = temp;

                        }

                        

                        // Print the answer to standard output(screen).

                        System.out.print("#" + test_case + " ");

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

                                System.out.print(Answer[i]);

                        }

                        System.out.println();

                }

                sc.close();

        }

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值