首页 > 代码库 > ACM,大数相加问题

ACM,大数相加问题

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,大数相加问题