4 / 8
Mar 2016

I dont know why I got wrong answer. :frowning: Can you help me?

#include <stdio.h>
#include <math.h>
int reversednum(int n)
{int i,j,a,d,b[5];
int c;
if (!n){return 0;}
b[0]=n/10000;
b[1]=(n%10000)/1000;
b[2]=(n%1000)/100;
b[3]=(n%100)/10;
b[4]=(n%10);
for (i=0;!b[i];i++){}
c=b[i];
for(a=1,j=i+1,d=1;j<5;j++,a++){
    d=d*10;
    c+=b[j]*d;
  }
return c;
}
void reversedsum(int a,int b)
{int c,d,e;
c=reversednum(a)+reversednum(b);
printf("%d\n",reversednum(c));
}
int main()
{int n=0,s,s2,m;
scanf("%d",&m);
  while (n<m)  {
    scanf("%d %d",&s,&s2);
    reversedsum(s,s2);
    n++;
  }
  return 0;
}
  • created

    Mar '16
  • last reply

    Mar '16
  • 7

    replies

  • 1.1k

    views

  • 2

    users

  • 1

    link

Which test cases did you check? What do you think is the maximal size of the input numbers?

I did check a lot of numbers. The output in all of them is ok. I dont know why it is wrong. It works with inputs since 0 to at least 10000.

It still not working in the test. When I use gcc in my pc I dont see nothing wrong in the code.

 #include <stdio.h>
    int reversednum(int n)
    {int i,j,a,d,b[7];
    int c;
    if (!n){return 0;}
    b[0]=n/1000000;
    b[1]=(n%1000000)/100000;
    b[2]=(n%100000)/10000;
    b[3]=(n%10000)/1000;
    b[4]=(n%1000)/100;
    b[5]=(n%100)/10;
    b[6]=(n%10);
    for (i=0;!b[i];i++){}
    c=b[i];
    for(a=1,j=i+1,d=1;j<7;j++,a++){
        d=d*10;
        c+=b[j]*d;
      }
    return c;
    }
    void reversedsum(int a,int b)
    {int c,d,e;
    c=reversednum(a)+reversednum(b);
    printf("%d\n",reversednum(c));
    }
    int main()
    {int n=0,s,s2,m;
    scanf("%d",&m);
      while (n<m)  {
        scanf("%d %d",&s,&s2);
        reversedsum(s,s2);
        n++;
      }
      return 0;
    }

Yes, but that is not what the code above generates at ideone.com. You should redesign your reversenum function. It can be far more simple and flexible.