1 / 29
Oct 2005

Well.. both your programs have the same output and they both contain some negative integers. They should be all positive, I think?

4 years later

I think my this part of code is giving me WA. Can anybody figure out where is problem?

CODE REMOVED. GOT AC

You're fairly certain that it doesn't work. Have you tested it? Write a brute-force checker that checks if i-j = substract(i,j). I did and there were about 500 hits with i <= 1000.

Thanks leppy
I tried that & found that it is working correct for 100000
but i am still getting WA
here is my code, can you plz check it

CODE REMOVED. GOT AC

Your problem is in your substract method. It does not work properly.

I know that leppy, but i am finding any mistake in that
can you tell where the problem is or can you provide some test case
that would be helpful for me
thanks in advance

The difficulty in solving these problems is coming up with the test cases. smile The code is just a tool. A WA is just a challenge to find the test case that breaks the program.

I ran a brute-force test on your substract(i,j) method for all pairs j <= i <= 1000 and found over 500 pairs that fail. You should try the same. If you still find no problems, post your brute-force checker and I'll help you debug that.

I am continuosly getting WA in this ques not because of overflow...i hav made my own bigint class in c++ and it is running absolutely fine for all the test cases inputted by me..i hav covered all the corner cases upto my knowledge but still it is giving WA on submission....someone plz give me some tricky test cases so that i can debug more my prog to find the error...
Thanxxx

Without seeing your code, it's nearly impossible to suggest someting. As you've already guessed there's nothing tricky about this problem.

Okay leppy then you tell me your id i will mail my code to u.plz help me out i am tired of figuring out where i am going wrong.

id? No thanks. You can post your code on the forum and delete it later. If you really want, you can send it as a private message right here on the forum.

8 months later

Hey,what should be the output for
3232
1233
I'm getting 2232 and 999..but that doesnt add up to 3232!

Removed

Hey,Thanks!I corrected that and a few other error too..still getting WA!!

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
char a[110],b[110],c[110],d[110];
void add()
{
     int i,j=strlen(a)-1,k=strlen(b)-1,t,r=0,u=0,s=max(strlen(a),strlen(b)),temp,flag;
     c[s+1]='\0';
     for(i=s;i>=0 && j>=0 && k>=0;i--)
     {
             t=a[j--]+b[k--]-48;
             temp=t-48+u;
             if((t-48+u)>9)
             {
                         r=(t-48+u)%10;
                         u=(t-48+u)/10;
                         c[i]+=r;
                         //c[i-1]+=u;
             }
             else
             {
              c[i]+=t-48+u;
              u=0;
             }
             if(i==1)
             c[0]+=u;
             //cout<<i<<" "<<a[j+1]<<" "<<b[k+1]<<" "<<c[i]<<" "<<r<<" "<<u<<" "<<temp<<endl;
     }
     while(j>=0)
             c[i--]+=a[j--]-48;     
}
void divide()
{
     int k=0,i=0,r=0,t;
     while(c[i]==48)
     i++;
     //cout<<"i t d[k] r\n";
     //cout<<c<<endl;
     for(;i<strlen(c);i++)
     {
           if(c[i]==48 && r==0)
           {
            d[k++]=48;
            t=0;
           }
           else
           {
           t=(r*10+c[i]-48);
           if(t==1 && i==strlen(c)-1)
           d[k++]=48;
           else
           {
           if(t<=1 && i<strlen(c)-1)
           {
            t=c[i]*10+c[++i]-480-48;
            //if(k!=0)
            d[k++]=48;
           }
            d[k++]=(t/2+48);
            r=t%2;
           }
           }
           //cout<<i<<" "<<t<<" "<<d[k-1]<<" "<<r<<endl;
     }
     d[k]='\0';
} 
void subtract()
{
     int d=strlen(a)-1,e=strlen(b)-1,s=max(strlen(a),strlen(b)),temp;
     c[s]='\0';
     int i;
     for(i=s-1;i>=0 && d>=0 && e>=0;i--)
     {
             if(a[d]<b[e])
             {
                          temp=d;
                          while(a[--temp]==48)
                          a[temp]=9+48;
                          a[temp]=a[temp]-1;
                          c[i]=a[d--]+10-b[e--]+48;
             }
             else
             c[i]=a[d--]-b[e--]+48;
     }
     while(d>=0)
                c[i--]=a[d--];
}
int main()
{
    int l=10;
    while(l--)
    {
              for(int t=0;t<105;t++)
                      c[t]=d[t]=48;
              scanf("%s%s",&a,&b);
              if(!strcmp(a,b))
              printf("%s\n0\n",a);
              else{
              add();
              divide();
              int i=0;
              while(d[i]==48)
              i++;
              while(d[i]!='\0')
              printf("%c",d[i++]);
              printf("\n");
              for(int t=0;t<105;t++)
                      c[t]=d[t]=48;
              subtract();
              divide();
              i=0;
              while(d[i]==48)
              i++;
              while(d[i]!='\0')
              printf("%c",d[i++]);
              printf("\n");}
    }
    return 0;
}

leppy,i tried all the test cases i could think of..still getting WA..could you please give me a test case for which the above code fails?

It didn't take me long to create a brute force program that systematically created many test cases for this problem and the correct answers according to your formula. Then I ran your program on that input file. Then I compared your results to the expected results. You'll find the answer there.