华为机试——两个超长正整数的加法 java

来源:互联网 发布:手机蓝牙网络共享电脑 编辑:程序博客网 时间:2024/05/16 10:58

题目描述:请设计一个算法完成两个超长正整数的加法。 

要求实现函数:

  void AddLongInteger(char * pcAddend, char * pcAugend, char * pcAddResult);

输入参数:

        char * pcAddend:加数

        char * pcAugend:被加数

        char * pcAddResult:加法结果

返回值:无

运行时间限制:1 Sec内存限制:128 MByte输入:

两个超长正整数的字符串

输出:

相加后结果的字符串

样例输入:
123456789123456789 123456789123456789
样例输出:
246913578246913578


[java] view plaincopy
  1. import java.util.Arrays;  
  2. import java.util.Scanner;  
  3.   
  4.   
  5. public class Main {  
  6.   
  7.     public static void main(String[] args) {  
  8.         // TODO Auto-generated method stub  
  9.           
  10.         Scanner sc = new Scanner(System.in);  
  11.           
  12.         char[] a1 = sc.next().toCharArray();  
  13.         char[] a2 = sc.next().toCharArray();  
  14.   
  15.         char[] result = AddLongInteger(a1,a2);  
  16.         for(int i = 0;i < result.length;i++){  
  17.             System.out.print(result[i]);  
  18.         }         
  19.   
  20.     }  
  21.   
  22.     /* 
  23.      * AddLongInteger(char[] a1, char[] a2)的思想: 
  24.      * 首先:a1[0]对应的是第一个被加数的最高位,a2[0]同样,所以做加法的时候应该从a1[a1.length-1]位开始,或者将a1前后转置 
  25.      * 然后加法的计算是:(a1[1]+a2[1]+进位)%10   进位=(a1[1]+a2[1]+进位)/10 
  26.      * 然后就是字符与数字之前的转换,千万不要搞混 
  27.      */  
  28.     public static char[] AddLongInteger(char[] a1, char[] a2){  
  29.           
  30.         int len=0;  
  31.         if(a1.length > a2.length){  
  32.             len = a1.length + 1;  
  33.         }else{  
  34.             len = a2.length + 1;  
  35.         }  
  36.           
  37.         char[] temp = new char[len];  
  38.         char[] result;  
  39.           
  40.         char[] b1 = new char[a1.length];  
  41.         char[] b2 = new char[a2.length];  
  42.           
  43.         for(int i = 0;i < a1.length;i++){  
  44.             b1[a1.length - 1 - i] = a1[i];  
  45.         }                 
  46.           
  47.         for(int i = 0;i < a2.length;i++){  
  48.             b2[a2.length - 1 - i] = a2[i];  
  49.         }  
  50.           
  51.         for(int i = 0; i < temp.length; i++){  
  52.             temp[i] = '0';  
  53.         }  
  54.           
  55.         for(int i = 0; i < len-1;i++){  
  56.             int res = 0;  
  57.             if(b1.length -1 - i >=0 && b2.length -1 - i >= 0){  
  58.                  res = b1[i] - '0' + b2[i] - '0';                             
  59.             }else if(b1.length -1 - i >= 0 && b2.length -1 - i < 0){  
  60.                  res = b1[i] - '0';                   
  61.             }else if(b1.length -1 - i < 0 && b2.length -1 - i >= 0){  
  62.                  res = b2[i] - '0';  
  63.             }  
  64.               
  65.             int value = temp[i]-'0'+res;  
  66.             temp[i] = (char) (value%10 + '0') ;               
  67.             temp[i+1] += value/10;  
  68. //          System.out.println(temp[i]);  
  69. //          System.out.println(temp[i+1]);  
  70.               
  71.         }  
  72.           
  73.           
  74.         while(temp[len - 1] == '0'){  
  75.             len--;  
  76.         }     
  77.         result = new char[len];  
  78.         int s = result.length;  
  79.           
  80.         for(int i = 0;i < s; i++){  
  81.             result[s-1-i] = temp[i];  
  82.         }  
  83.           
  84.         return result;  
  85.     }  
  86. }  
0 0
原创粉丝点击