读书人

[新人]杭电acm1002

发布时间: 2013-09-05 16:02:06 作者: rapoo

[新人求助]杭电acm1002
原题:
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



#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
int num;
vector<string> vecLeft, vecRight;
string left, right;
cin >> num;
if (num <1){
system("pause");
return -1;
}
if (num > 20){
system("pause");
return -1;
}
for (int i = 0; i != num; ++i){
cin >> left >> right;
vecLeft.push_back(left);
vecRight.push_back(right);
}
vector<string>::iterator iterLeft = vecLeft.begin(), iterRight = vecRight.begin();
for ( int i = 0; i != num; ++i){
int temp;
string tempa, tempb;
tempa = *iterLeft ++ ;
tempb = *iterRight ++ ;


reverse(tempa.begin(),tempa.end());
reverse(tempb.begin(),tempb.end());
//cout << tempa << " " << tempb << endl;
int con, mark, bita, bitb, carry = 0;
string sum, stra,strb;
if (tempa.size() < tempb.size()){
con = tempa.size();
mark = 0;
}
else{
con = tempb.size();
mark = 1;
}
for (int i = 0; i < con; ++i){
stra = tempa[i];
strb = tempb[i];
bita = atoi(stra.c_str());
bitb = atoi(strb.c_str());
sum.push_back('0' + (bita + bitb) % 10 + carry);
carry = (bita + bitb) / 10;
}
//cout << carry << endl;
if (mark == 0){
for (int i = con; i != tempb.size(); ++i){
strb = tempb[i];
bita = carry;
bitb = atoi(strb.c_str());
sum.push_back('0' + (bita + bitb) % 10);
carry = (bita + bitb) / 10;
}
}else{
for (int i = con; i != tempa.size(); ++i){
stra = tempa[i];
bita = atoi(stra.c_str());
bitb = carry;
sum.push_back('0' + (bita + bitb) % 10);
carry = (bita + bitb) / 10;
}
}
if ( carry == 1)
sum.push_back('0'+ carry);
reverse(tempa.begin(), tempa.end());
reverse(tempb.begin(), tempb.end());
reverse(sum.begin(), sum.end());
cout << "Case " << i + 1 << ":" <<endl;
cout << tempa << " + " << tempb << " = " << sum << endl;
cout << endl;
}
system("pause");
return 0;
}



一只是wrong answer,求大神指点。。。 ACM c++
------解决方案--------------------


仅供参考:

#include <stdio.h>
#include <string.h>
#define MAXLEN 1000
char a1[MAXLEN];
char a2[MAXLEN];
static int v1[MAXLEN];
static int v2[MAXLEN];
static int v3[MAXLEN];
int i,j,n,L,z;
void main(void) {
scanf("%d",&n);
for (j=0;j<n;j++) {
scanf("%s%s",a1,a2);

L=strlen(a1);
for (i=0;i<L;i++) v1[i]=a1[L-1-i]-'0';

L=strlen(a2);
for (i=0;i<L;i++) v2[i]=a2[L-1-i]-'0';

for (i=0;i<MAXLEN;i++) v3[i]=v1[i]+v2[i];

for (i=0;i<MAXLEN;i++) {
if (v3[i]>=10) {
v3[i+1]+=v3[i]/10;
v3[i]=v3[i]%10;
}
}

printf("Case %d:\n", j+1);
printf("%s + %s = ", a1, a2);

z=0;
for (i=MAXLEN-1;i>=0;i--) {
if (z==0) {
if (v3[i]!=0) {
printf("%d",v3[i]);
z=1;
}
} else {
printf("%d",v3[i]);
}
}
if (z==0) printf("0");

printf("\n");


}
}
//Sample Input
//3
//0 0
//1 2
//112233445566778899 998877665544332211
//
//Sample Output
//Case 1:
//0 + 0 = 0
//Case 2:
//1 + 2 = 3
//Case 3:
//112233445566778899 + 998877665544332211 = 1111111111111111110

读书人网 >C++

热点推荐