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

解读1:
  将输入的两个加数整理一下,比如:
1)99 和1整理成099和001的模式
2)8 和7整理成08和07的模式
相当于模拟算式运算
  【】【】【】
+【】【】【】

   【】【】【】
最后输出时稍作判断即可
代码:
#include<iostream>
#include<cstring>
using namespace std;
char a[1005],b[1005],c[1005],d[1005];
int main()
{ 
  int T;
  cin>>T;
  int x,y,temp;
  int la,lb,l,k=0;
  while(T--)
  {
      k++;//计数第几个例子 
      cin>>a; 
      cin>>b;//加数和被加数 
  
    la=strlen(a);
    lb=strlen(b);//加数和被加数的长度 
    l=la>lb?la:lb;//l为选取其中的最大长度 
    
    for(int i=la;i>=0;i--)
      a[i+l-la+1]=a[i];
    for(int i=lb;i>=0;i--)
      b[i+l-lb+1]=b[i];
    //将加数和被加数往后移以保持个位对齐 
    for(int i=0;i<=l-la;i++)
      a[i]='0';
    for(int i=0;i<=l-lb;i++)
      b[i]='0';
    //高位空缺的补'0'
    temp=0;//temp代表进位 
    c[l+1]='\0';//存储加数之和的数组记得添结束符 
    for(int i=l;i>=0;i--)
    {
        x=(a[i]-'0');
        y=(b[i]-'0');
        c[i]=(x+y+temp)%10+'0';
        temp=(x+y+temp)/10;    
    }//计算 
    cout<<"Case "<<k<<":"<<endl;
    cout<<a+l-la+1<<" + "<<b+l-lb+1<<" = ";
    if(c[0]=='0')//输出 
      cout<<c+1<<endl;
    else
      cout<<c<<endl;
    if(T!=0)cout<<endl;     
  }
} 

 


解读2:
  四个字节的整型最多能存下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谢谢

至于乘法和除法之后再补充!!!

   
 
posted @ 2017-05-10 23:40  hui666  阅读(2351)  评论(0编辑  收藏  举报