Java 大数加法HdAcm1002

Java 大数加法HdAcm1002
 1 import java.util.Scanner;
 2 
 3 
 4 public class Main {
 5     public static void main(String[] args) {
 6         Scanner cin = new Scanner(System.in);
 7         int n = cin.nextInt();
 8         String num1 = "";
 9         String num2 = "";
10         int m = 1;
11         while(m <= n){
12             num1 = cin.next();
13             num2 = cin.next();
14             int len1 = num1.length();
15             int len2 = num2.length();
16             int lenMin = len1 < len2 ? len1:len2;
17             int lenMax = len1 > len2 ? len1:len2;
18             char[] res = new char[lenMax+1];
19             //开始进位 为 0
20             int jinwei = 0;
21             //从两个字符串的尾部开始计算
22             for(int i = 1; i <= lenMin; i++){
23                 int a = num1.charAt(len1 - i) - ‘0‘;
24                 int b = num2.charAt(len2 - i) - ‘0‘;
25                 if(a + b + jinwei <= 9){
26                     //res 的长度为 lenMax
27                     res[lenMax + 1 - i] = (char)(a + b + jinwei + ‘0‘);
28                     jinwei = 0;
29                 }
30                 else{
31                     res[lenMax + 1 - i] = (char)(a + b - 10 + jinwei + ‘0‘);
32                     jinwei = 1;
33                 }
34             }
35             //将长字符串的值赋给res
36             if(lenMax == len1){
37                 for(int j = len1 - lenMin - 1; j >= 0;j--) {
38                     int a = num1.charAt(j) - ‘0‘;
39                     if( a + jinwei <= 9){
40                         res[j+1] = (char)(a + jinwei + ‘0‘); 
41                         jinwei = 0;
42                     }
43                     else{
44                         res[j+1] = (char)(a + jinwei - 10 + ‘0‘);
45                         jinwei = 1;
46                     }
47                 }
48             }
49             else{
50                 for(int j = len2 - lenMin - 1; j >= 0;j--) {
51                     int a = num2.charAt(j) - ‘0‘;
52                     if( a + jinwei <= 9){
53                         res[j+1] = (char)(a + jinwei + ‘0‘); 
54                         jinwei = 0;
55                     }
56                     else{
57                         res[j+1] = (char)(a + jinwei - 10 + ‘0‘);
58                         jinwei = 1;
59                     }
60                 }
61             }
62             System.out.println("Case" + " " + m + ":");
63             if(jinwei == 1){
64                 res[0] = ‘1‘;
65                 System.out.println(num1 + " " + "+" + " " + num2 + " " + "=" + " " + String.valueOf(res));
66             }
67             else {
68                 System.out.println(num1 + " " + "+" + " " + num2 + " " + "=" + " " + String.valueOf(res, 1, lenMax));
69             }
70             if(m < n){
71                 System.out.println();
72             }
73             m++;
74         }
75         cin.close();
76     }
77 }
Java 大数加法HdAcm1002

Java 大数加法HdAcm1002,布布扣,bubuko.com

Java 大数加法HdAcm1002

上一篇:VC++ 学习笔记(一): 如何开始


下一篇:JavaScript复习--评分控件(类似于淘宝给好差平的星星)