You are not logged in. Please login at www.codechef.com to post your questions!

×

JULKA Prob on SPOJ

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 <iostream>

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;

}

asked 16 Jul '14, 04:17

apoorv024's gravatar image

5★apoorv024
111
accept rate: 0%


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.

link

answered 22 Oct '14, 21:37

genius_gunji's gravatar image

2★genius_gunji
1
accept rate: 0%

even this code is giving wrong answer. Please help me to know why it is giving wrong answer :-

include <iostream>

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[b])
        {

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

            div[a-1]--;

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

        }
        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[b] + 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;

}

link

answered 22 Oct '14, 21:43

genius_gunji's gravatar image

2★genius_gunji
1
accept rate: 0%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×1,136
×10

question asked: 16 Jul '14, 04:17

question was seen: 2,541 times

last updated: 22 Oct '14, 21:43