JULKA Prob on SPOJ

julka
spoj

#1

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*=s*-‘0’;

	cin>>s;
	sizey = s.length();
	for(i=0;i<sizey;i++)
	y*=s*-'0';
/*	for(i=0;i<sizey;i++)
	cout<<y*;*/
	i=0;
	j=0;
	if(x[0]==1) {
		left=1;
		i++;
	}
	else left=0;
	
	for(;i<sizex;i++){
		x1[j++]=(left*10+x*)/2;
		left = (left*10+x*)%2;
	}
	if(x[0]==1) sizex1=sizex-1;
	else sizex1=sizex;
	/*for(i=0;i<sizex1;i++)
	cout<<x1*;
	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*)/2;
		left = (left*10+y*)%2;
	}
	if(y[0]==1) sizey1=sizey-1;
	else sizey1=sizey;
/*
	for(i=0;i<sizey1;i++)

	cout<<y1*;
	cout<<endl;
*/
	left=0;
	j=sizex1;
	k=sizex1-1;
	for(i=sizey1-1;i>=0;i--){
		add[j]=(x1[k]+y1*+left)%10;
		left = (x1[k]+y1*+left)/10;
		j--;
		k--;
	}
	
	for(i=sizex1-sizey1;i>=1;i--){
		add*=(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*;
	cout<<endl;
*/	
	left=0;
	j=sizex1-1;
	k=sizex1-1;
	for(i=sizey1-1;i>=0;i--){
		if(x1[k]<y1*){
			sub[j--]=x1[k--]+10-y1*;
			x1[k]--;
		}
		else
		sub[j--]=x1[k--]-y1*;
	}
	for(i=sizex1-sizey1-1;i>=0;i--)
		sub*=x1*;
	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*;
			cout<<endl;
		}
		else {
			for(i=1;i<=sizeadd;i++)
			cout<<add*;
			cout<<endl;
		}
		if(sizesub>=sizex1){
			for(i=0;i<sizesub;i++)
			cout<<sub*;
			cout<<endl;
		}
		else {
			for(i=1;i<=sizesub;i++)
			cout<<sub*;
			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* = add*+left;
			left = add*/10;
			add* = add*%10;
		}
		}
		else {
			left = (add[sizeadd]+1)/10;
		add[sizeadd] = (add[sizeadd]+1)%10;
		

		for(i=sizeadd-1;i>=1;i--){
			add* = add*+left;
			left = add*/10;
			add* = add*%10;
		}
		if(left!=0) {
			add[0] = left;
			sizeadd = sizex1+1;
		}
		else
		sizeadd = sizex1;
		}
		
		if(sizeadd>sizex1){
			for(i=0;i<sizeadd;i++)
			cout<<add*;
			cout<<endl;
		}
		else {
			for(i=1;i<=sizeadd;i++)
			cout<<add*;
			cout<<endl;
		}
		if(sizesub>=sizex1){
			for(i=0;i<sizesub;i++)
			cout<<sub*;
			cout<<endl;
		}
		else {
			for(i=1;i<=sizesub;i++)
			cout<<sub*;
			cout<<endl;
		}
		
	}
	
}
return 0;

}


#2

try 100000 80 as a test case. It gives 50040 50-160 as output.There might be some logical error in the code for subtraction due to which -1 is coming as answer.


#3

even this code is giving wrong answer. Please help me to know why it is giving wrong answer :-
#include
#include<string.h>
using namespace std;

int main()
{
char num[200],more[200];
int div[200],ans[200],sub[200],add[200];
int test,ind,div1;

for(int i=0;i<10;i++)
{
    cin>>num>>more;





    ind =-1;
    int len1 = strlen(num) - 1;
    int len2 = strlen(more) - 1;
    for(int j=0;j<=len1;j++)
    {
        div[j] = num[j] - 48;
    }
    for(int j=0;j<=len2;j++)
    {
        sub[j] = more[j] - 48;
    }

int k=0,rem=0,b=len2,a=len1;

    for(int z=len2;z>=0;z--)
    {
        if(div[a]<sub**)
        {

            div[a] = div[a] - sub** + 10;

            div[a-1]--;

        }
        else
        {
            div[a] = div[a] - sub**;

        }
        a--;b--;
    }

while(div[a]<0)
{
div[a]+=10;
div[a-1]–;
a–;
}

    while(k<=len1)
    {
        div[k] = div[k] + 10*rem;
        if(div[k]==1 && k!=len1)
        {
            div1 = 10*div[k] + div[++k];
        }
        else if(div[k]==0)
        {
            ans[++ind] = 0;
            if(k==len1)
                break;
            else
                k++;
            continue;
        }
        else
        {
        div1=div[k];
        }
        ans[++ind] = div1/2;
        rem=div1%2;
        k++;
    }
    a=ind,b=len2;
    int sum,carry=0,index=-1;
    while(true)
    {
        if(a<0 || b<0)
            break;
        sum = ans[a] + sub** + carry;
        add[++index] = sum%10;
        carry = sum/10;
            a--;b--;
    }

    if(ind>len2)
    {
        a++;

            while(a>0)
            {
            sum = ans[--a] + carry;
            add[++index] = sum%10;
            carry = sum/10;
            }
    }
    else if(len2>ind)
    {
        b++;
        while(b>0)
        {
            sum = sub[--b] + carry;
            add[++index] = sum%10;
            carry = sum/10;
        }
    }

    while(carry>0)
    {
        add[++index] = carry%10;
        carry = carry/10;
    }


    while(add[index]==0)
    {
        index--;
    }

    while(index>=0)
    {
        cout<<add[index];
        index--;
    }
    cout<<endl;



    int x=0;
    while(ans[x]==0)
    {
        x++;
    }

        while(x<=ind)
        {
            cout<<ans[x];
            x++;
        }

        cout<<endl;




}






return 0;

}