I was trying to solve the JULKA prob on SPOJ in C++.
The code is working fine on ideone and giving correct results for my test cases too!
But when I submit it, I get WA!
Please help me out. I am completely frustrated now!
Here is my code:
#include
using namespace std;
int main() {
int c = 10,i,sizex,sizey,sizex1,sizey1,sizeadd,sizesub,j,k;
int x[105],y[105],x1[105],y1[105],add[105],sub[105],left;
string s;
while(c–){
cin>>s;
sizex = s.length();
for(i=0;i<sizex;i++)
x[i]=s[i]-‘0’;
cin>>s;
sizey = s.length();
for(i=0;i<sizey;i++)
y[i]=s[i]-'0';
/* for(i=0;i<sizey;i++)
cout<<y[i];*/
i=0;
j=0;
if(x[0]==1) {
left=1;
i++;
}
else left=0;
for(;i<sizex;i++){
x1[j++]=(left*10+x[i])/2;
left = (left*10+x[i])%2;
}
if(x[0]==1) sizex1=sizex-1;
else sizex1=sizex;
/*for(i=0;i<sizex1;i++)
cout<<x1[i];
cout<<endl;
*/
i=0;
j=0;
if(y[0]==1) {
left=1;
i++;
}
else left=0;
for(;i<sizey;i++){
y1[j++]=(left*10+y[i])/2;
left = (left*10+y[i])%2;
}
if(y[0]==1) sizey1=sizey-1;
else sizey1=sizey;
/*
for(i=0;i<sizey1;i++)
cout<<y1[i];
cout<<endl;
*/
left=0;
j=sizex1;
k=sizex1-1;
for(i=sizey1-1;i>=0;i--){
add[j]=(x1[k]+y1[i]+left)%10;
left = (x1[k]+y1[i]+left)/10;
j--;
k--;
}
for(i=sizex1-sizey1;i>=1;i--){
add[i]=(x1[i-1]+left)%10;
left = (x1[i-1]+left)/10;
}
if(left!=0) {
add[0]=left;
sizeadd=sizex1+1;
}
else sizeadd=sizex1;
/* for(i=1;i<=sizeadd;i++)
cout<<add[i];
cout<<endl;
*/
left=0;
j=sizex1-1;
k=sizex1-1;
for(i=sizey1-1;i>=0;i--){
if(x1[k]<y1[i]){
sub[j--]=x1[k--]+10-y1[i];
x1[k]--;
}
else
sub[j--]=x1[k--]-y1[i];
}
for(i=sizex1-sizey1-1;i>=0;i--)
sub[i]=x1[i];
if(sub[0]==0) sizesub=sizex1-1;
else sizesub=sizex1;
if(x[sizex-1]%2==0){
if(sizeadd>sizex1){
for(i=0;i<sizeadd;i++)
cout<<add[i];
cout<<endl;
}
else {
for(i=1;i<=sizeadd;i++)
cout<<add[i];
cout<<endl;
}
if(sizesub>=sizex1){
for(i=0;i<sizesub;i++)
cout<<sub[i];
cout<<endl;
}
else {
for(i=1;i<=sizesub;i++)
cout<<sub[i];
cout<<endl;
}
}
else{
if(sizeadd>sizex1){
left = (add[sizeadd-1]+1)/10;
add[sizeadd-1] = (add[sizeadd-1]+1)%10;
for(i=sizeadd-2;i>=0;i--){
add[i] = add[i]+left;
left = add[i]/10;
add[i] = add[i]%10;
}
}
else {
left = (add[sizeadd]+1)/10;
add[sizeadd] = (add[sizeadd]+1)%10;
for(i=sizeadd-1;i>=1;i--){
add[i] = add[i]+left;
left = add[i]/10;
add[i] = add[i]%10;
}
if(left!=0) {
add[0] = left;
sizeadd = sizex1+1;
}
else
sizeadd = sizex1;
}
if(sizeadd>sizex1){
for(i=0;i<sizeadd;i++)
cout<<add[i];
cout<<endl;
}
else {
for(i=1;i<=sizeadd;i++)
cout<<add[i];
cout<<endl;
}
if(sizesub>=sizex1){
for(i=0;i<sizesub;i++)
cout<<sub[i];
cout<<endl;
}
else {
for(i=1;i<=sizesub;i++)
cout<<sub[i];
cout<<endl;
}
}
}
return 0;
}