1. 程式人生 > >ACM,大數相加問題

ACM,大數相加問題

ont there 討論 ble ddc != posit 轉換 swap

A + B Problem II

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 316067 Accepted Submission(s): 61349

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
解讀: 四個字節的整型最多能存下10*10大小的整數,長整型也不足以存下,因此考慮用數組來存,相加之和則用整型數組存放 1、先把加數和被加數數輸入到字符數組中 2、再用循環將對每位進行處理再相加 分三種情況來討論(見代碼)
切記最高位進位問題 3、最後輸出
代碼:
#include<iostream>
#include<string.h>
using namespace std;
char a[1000],b[1000];//加數和被加數 
int c[1000];//
int main()
{
    int T;//記錄共有幾個例子 
    while(scanf("%d",&T))
    {
        int i=1;//記錄當前是第幾個例子 
        int m,n;//用來存放每個字符轉化成的數字 
        while(T--)
        {//處理每個例子 
           int j=0;//記錄和的位數 
           scanf("%s%s",a,b);
           m=strlen(a);//計算出串長 
           n=strlen(b);
           int temp=0;//用來存儲進位 
           for(m--,n--;m>=0||n>=0;)
           {//逐位相加用循環, 
                 if(m>=0&&n>=0)
                 {
                     int x,y;
                   x=a[m]-0;//轉換成整型 
                   y=b[n]-0;
                   c[j]=(x+y+temp)%10;
                   temp=(x+y+temp)/10;
                   m--;n--;j++;
              }
                 if(m>=0&&n<0)
                 {
                     int x;
                     x=a[m]-0;//轉換成int 
                     c[j]=(x+temp)%10;
                     temp=(x+temp)/10;
                     m--;j++;
              }
              if(m<0&&n>=0)
                 {
                     int y;
                     y=b[n]-0;
                     c[j]=(y+temp)%10;
                     temp=(y+temp)/10;
                     n--;j++;
              }
           }
           if(temp!=0)
           {//當最高位還有進位時 
                c[j]=temp;
                j++;
           }
           cout<<"Case "<<i<<":"<<endl;
           cout<<a<<" + "<<b<<" = ";
           for(;j-1>=0;j--)
             cout<<c[j-1];
           cout<<endl;
           i++;
           if(i<T)
           cout<<endl;
        }
    }
 } 

以下再給出另一種解法(畢竟思路要開闊嘛,所以就收錄了接下來的這個代碼)

#include<iostream>  
#include<string>  
using namespace std;  
char add(char temps,char tempstr,int &tempaddc)  
{  
    int temp;   
    temp = (temps - 0) + (tempstr - 0) + tempaddc;  //實現進位。  
    tempaddc = temp / 10;  // 算出進位數  
    return temp % 10 + 0;  
}  
int main()  
{  
    string str,s;  
    int tempaddc;  
    char c;  
    int n,lenstr,lens;  
    while(cin>>n)  
    {  
        while(n--)  
        {  
            cin>>str>>s;  
            tempaddc = 0;  
            c = 0;  
            if(s.length() > str.length())swap(s,str);  
            lenstr = str.length();  
            lens = s.length();  
            lens--;  
            lenstr--;  
            while (lenstr >= 0)  
            {  
                if(lens < 0) str[lenstr] = add(c,str[lenstr],tempaddc);    //字符串長度較長部分的計算  
                else  
                {  
                    str[lenstr] = add(s[lens],str[lenstr],tempaddc);  
                    lens--;  
                }  
                lenstr--;  
            }  
            cout<<str<<endl;  
        }  
    }  
    return 0;  
}  

如有疑問請指出,O(∩_∩)O謝謝

至於乘法和除法之後再補充!!!

ACM,大數相加問題