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謝謝
至於乘法和除法之后再補充!!!