Hello everybody, i am new here.
I need a help to check my code. i did below problem.
Your task is to write a program which reads two integers (more than or equal to zero) which have at most 80 digits, and prints a sum of these integers.
If given integers or the sum have more than 80 digits, print "overflow".
Input
Input consists of several datasets. In the first line, the number of datasets N is given. Each dataset consists of 2 lines:
The first integer
The second integer
Output
For each dataset, print the sum of given integers in a line.
Sample Input
2
1000
800
9999999999999999999999999999999999999999
1
Output for the Sample Input
1800
10000000000000000000000000000000000000000
i wrote code below and submit it to a website which can automatically rate my code. but the website displayed that my code returned wrong answer. i tested my code many times with my sample cases and it worked well. so maybe there are
cases i missed.
by the way, i also don't understand why i should input dataset if the problem stated that i should add only 2 integers. with my algorithm below, dataset become useless.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
#include<iostream>
#include<string>
using namespace std;
int main(){
int arrnum1[82],arrnum2[82],arrnum3[82],dataset;
string num1,num2;
cin>>dataset;
while(1){
if (!(cin>>num1>>num2))break;
if (num1.length()<=80 && num2.length()<=80){
for (int i=0;i<82;i++){
arrnum1[i]=0;
arrnum2[i]=0;
arrnum3[i]=0;
}
for (int i=1;i<num1.length()+1;i++){
char c =num1[num1.length()-i];
arrnum1[i]=c-'0';
}
for (int i=1;i<num2.length()+1;i++){
char c =num2[num2.length()-i];
arrnum2[i]=c-'0';
}
for (int i=1;i<82;i++){
int k=0;
if (arrnum3[i-1]>9){
arrnum3[i-1]=arrnum3[i-1]-10;
k=1;
}
arrnum3[i]=arrnum1[i]+arrnum2[i]+k;
}
if (arrnum3[81]==0){
int nil=0;
for (int i=80;i>0;i--){
if (arrnum3[i]!=0||nil!=0){
cout<<arrnum3[i];
nil=1;
}
}
}else cout<<"overflow";
cout<<endl;
}
else cout<<"overflow"<<endl;
}
return 0;
}
|