2 / 5
Mar 2016

hello, please help me to find out why this solution gets WA. here is the problem: http://www.spoj.com/problems/JULKA/8
please check out for some values. it seems right to me

#include <iostream>
#include <string>
using namespace std;

int main() {
	for(int w=0; w<10; ++w){
	string result="";
		string sum;
		string more;
		cin>>sum;
		cin>>more;
		int carry=0;
		for(int j=0; j<more.length(); ++j){
			if(sum[sum.length()-1-j]-more[more.length()-1-j]-carry<0){
				result=(char)(sum[sum.length()-1-j]-more[more.length()-1-j]-carry+10+'0')+result;
				carry=1;
			}
			else{
				result=(char)(sum[sum.length()-1-j]-more[more.length()-1-j]-carry+'0')+result;
				carry=0;	
			}
		}
		
		for(int k=sum.length()-more.length()-1; k>=0; --k){
			if(sum[k]-carry-'0'<0){
				result=(char)(sum[k]-carry+10)+result;
				carry=1;
			}
			else{
				result=(char)(sum[k]-carry)+result;
				carry=0;
			}
		}
		int k=0;
		while(true){
			if(result[k]!=(char)('0') || result.length()==1){
				break;	
			}
			else{
				result.erase(k, 1);
			}
		}
		
		string candies1="";
		bool isremainder=false;
		for(int i=0; i<result.length(); ++i){
			if((result[i]-'0')%2==0){
			if(!isremainder){
				candies1=candies1+(char)((result[i]-'0')/2+'0');
				isremainder=false;
			}
			else{
				candies1=candies1+(char)((10+result[i]-'0')/2+'0');
				isremainder=false;
			}
			}
			else{
			if(!isremainder){
				if((10*(result[i]-'0')+result[i+1]-'0')/20!=0)candies1=candies1+(char)((10*(result[i]-'0')+result[i+1]-'0')/20+'0')+(char)(((10*(result[i]-'0')+result[i+1]-'0')/2)%10+'0');
				else candies1=candies1+(char)(((10*(result[i]-'0')+result[i+1]-'0')/2)%10+'0');
				if((result[i+1]-'0')%2==1)isremainder=true;
				++i;
				}
			else{
				candies1=candies1+(char)(((10+result[i]-'0')/2)%10+'0');
				if((result[i]-'0')%2==0){
					isremainder=false;
				}
			}
		}
		}
		k=0;
		while(true){
			if(candies1.length()==1 || candies1[k]!='0')break;
			else candies1.erase(k, 1);
		}
		string candies2="";
		int carry2=0;
		for(int i=0; i<max(candies1.length(), more.length()); ++i){
			if((int)(candies1.length()-1-i)>=0 && (int)(more.length()-1-i)>=0){
				if(candies1[candies1.length()-1-i]-'0'+more[more.length()-1-i]-'0'+carry2>=10){
					candies2=(char)((candies1[candies1.length()-1-i]-'0'+more[more.length()-1-i]-'0'+carry2)%10+'0')+candies2;
					carry2=1;
				}
				else{
					candies2=(char)((candies1[candies1.length()-1-i]-'0'+more[more.length()-1-i]-'0'+carry2)%10+'0')+candies2;
					carry2=0;
				}
			}else if((int)(candies1.length()-1-i)>=0 && (int)(more.length()-1-i)<0){
					if(candies1[candies1.length()-1-i]-'0'+carry2>=10){
						candies2=char((candies1[candies1.length()-1-i]-'0'+carry2)%10+'0')+candies2;
						carry2=1;
					}
					else{
						candies2=char((candies1[candies1.length()-1-i]-'0'+carry2)%10+'0')+candies2;
						carry2=0;
					}
			}else if((int)(candies1.length()-1-i)<0 && (int)(more.length()-1-i)>=0){
					if(more[more.length()-1-i]-'0'+carry2>=10){
						candies2=char((more[more.length()-1-i]-'0'+carry2)%10+'0')+candies2;
						carry2=1;
					}
					else{
						candies2=char((more[more.length()-1-i]-'0'+carry2)%10+'0')+candies2;
						carry2=0;
					}
			}
		}
		if(carry2==1)candies2=string("1")+candies2;
		cout<<candies2<<endl;
		cout<<candies1<<endl;
	}
return 0;
}
  • created

    Mar '16
  • last reply

    Mar '16
  • 4

    replies

  • 1.6k

    views

  • 2

    users

  • 2

    links

Your code is broken and does not compile. Please edit your post, select the complete code and click preformatted.

If it is too difficult to select the code: you can use triple backticks(```) to separate normal text from code (and vice versa). Of course [code]...[/code] will work, too.

ok. Got it :sweat:. All you have to do is: write a test program and run though all possible inputs using integer data type and classical plus/minus division operations. You will find a case where your code fails with only 3 digits.