713 - Adding Reversed Numbers

All about problems in Volume VII. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

713

Postby murtaza » Tue Jul 20, 2004 7:51 pm

I saw all the help on prob.713 and tried all the suggested inputs .... but still i keep getting WA.
Can someone plz lokk at my code below and suggest.......

C code:
---------

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

/*find the revesed sum of 2 numbers .... numbers r upto 200 digits long!! */

char * reverse(char *,char *);
char * add(char *,char *,char *);

char num1[1001],num2[1001];
main()
{
char *rev1, *rev2, *result,* reversed;
int numCases,k = 0,flag = 0,j = 0,i;
rev1 = NULL;
rev2 = NULL;
result = NULL;
reversed = NULL;

scanf("%d",&numCases);
for(i = 0;i<numCases;i++)
{
scanf("%s %s",num1,num2);
rev1 = reverse(num1,rev1);
rev2 = reverse(num2,rev2);
result = add(rev1,rev2,result);
reversed = reverse(result,reversed);
if(rev1[0] == '\0' && rev2[0] == '\0')
printf("0\n");
else
printf("%s\n",reversed);
}
}


char * reverse(char * num,char * rev)
{
int len,i,flag = 0,j; /*if flag is '0' then no non-zero number has
been encountered so far*/
char *temp;
len = strlen(num);
temp = (char *)malloc(sizeof(char) * len);

j = 0;
for(i = len-1;i>-1;i--)
{
if(flag==0)
{
if(num[i] == '0')continue;
}
flag = 1;
temp[j] = num[i];
j++;
}
temp[j] = '\0';
rev = temp;
return temp;
}

char * add(char * op1,char * op2, char * res)
{
int len1,len2,i,j;
int carry = 0; /*this flag is set when there is a carry generated from the lower digits*/
char * temp,tp;
len1 = strlen(op1);
len2 = strlen(op2);

if(len1>len2)
{
temp = (char *)malloc(sizeof(char) * len1);
i = len1 + 1;
}
else
{
temp = (char *)malloc(sizeof(char) * len2);
i = len2 + 1;
}

temp[i] = '\0';
i--;
len1--;
len2--;
while(len1>= 0 && len2 >= 0)
{
if((op1[len1] - '0' + op2[len2] - '0' + carry) >9)
{/*there is a carry*/
temp[i] = op1[len1] - '0' + op2[len2] + carry - 10;
carry = 1;
}
else
{
temp[i] = op1[len1] + op2[len2] - '0' + carry;
carry = 0;
}
len1--;
len2--;
i--;
}
if(len1<0 && len2<0)
{
if(carry)
{
temp[i] = '1';
i--;
}
}
else if(len1<0)
{
while(len2>=0)
{
if((op2[len2] - '0' + carry) > 9)
{
temp[i] = op2[len2] + carry - 10 ;
carry = 1;
}
else
{
temp[i] = op2[len2] + carry;
carry = 0;
}
len2 -- ;
i--;
}
if(carry)
{
temp[i] = '1';
i--;
}
}
else
{
while(len1>=0)
{
if((op1[len1] - '0' + carry) > 9)
{
temp[i] = op1[len1] + carry - 10;
carry = 1;
}
else
{
temp[i] = op1[len1] + carry;
carry = 0;
}
len1--;
i--;
}
if(carry){temp[i] = '1';i--;}
}
if(i==0)
{
i = 1;
while(temp[i]!='\0')
{ temp[i-1] = temp[i];
/*printf("110 : %c\n",temp[i]);*/
i++;
}
temp[i-1]='\0';
}
res = temp;
return temp;
}



Thanks in advance
murtaza
New poster
 
Posts: 7
Joined: Tue Jul 20, 2004 7:42 pm
Location: India

Postby Piotrek Mazur » Sat Jul 31, 2004 7:27 pm

Can anyone give more sample input & output for this problem? I don't understand cleary when I have to replace zeros (leading zeros after adding, as the problem description says, and I tried to remove and not remove trailing zeros before adding).
Piotrek Mazur
New poster
 
Posts: 17
Joined: Thu Jul 15, 2004 10:55 am
Location: Poland, Rzeszow University of Technology

Postby Piotrek Mazur » Sun Aug 01, 2004 11:07 am

I have tested judge's input of leading and trailing zeroes: and I think there are no test cases with it (but input like '0 0' can be).
Piotrek Mazur
New poster
 
Posts: 17
Joined: Thu Jul 15, 2004 10:55 am
Location: Poland, Rzeszow University of Technology

Postby Piotrek Mazur » Fri Aug 13, 2004 10:39 pm

Forgot about my post above - I had one little error. Now is AC :)
Piotrek Mazur
New poster
 
Posts: 17
Joined: Thu Jul 15, 2004 10:55 am
Location: Poland, Rzeszow University of Technology

Postby Wei » Tue Sep 28, 2004 9:09 am

I don't know why~~
I have the right answers for these test datas...
But I still get a WA~~~
Could somebody give me some special test datas??
Wei
New poster
 
Posts: 23
Joined: Sat Jul 24, 2004 5:37 pm

Postby Eduard » Tue Sep 28, 2004 10:53 am

Hello
I think you know that you must use big integers.Here are some tests.
INPUT
Code: Select all
14
123123 12313213213
11111111 111111111111
565656565656 45646546
1 1
232 2323
123 13
1233333333333 123333333333
123123 1323333333
12333333333333 123333333333333
11 11
12 12
15 15
10 10
01 01

OUTPUT
Code: Select all
24625513213
222222221111
912122036656
2
4643
253
2466666666663
2554563333
246666666666663
22
24
201
20
2
Last edited by Eduard on Mon Oct 04, 2004 8:19 pm, edited 2 times in total.
someone who like to solve informatic problems.
http://acm.uva.es/cgi-bin/OnlineJudge?AuthorInfo:29650
Eduard
Experienced poster
 
Posts: 183
Joined: Fri Sep 26, 2003 2:54 pm
Location: Armenia,Yerevan

Postby Wei » Mon Oct 04, 2004 3:53 pm

Well~~Thx for giving me so many test datas.
But I still got WA~~
And I didn't know why when input were 10 10, the output should be 20???
Wei
New poster
 
Posts: 23
Joined: Sat Jul 24, 2004 5:37 pm

Postby Eduard » Mon Oct 04, 2004 8:17 pm

It is interesting for me too.May be My AC solution is not complately right.Or may be 10+10 -----> 01+01=02 ----->20. :(
someone who like to solve informatic problems.
http://acm.uva.es/cgi-bin/OnlineJudge?AuthorInfo:29650
Eduard
Experienced poster
 
Posts: 183
Joined: Fri Sep 26, 2003 2:54 pm
Location: Armenia,Yerevan

Postby WR » Tue Oct 05, 2004 3:41 pm

My accepted solution returns 2 for both 10, 10 and 01, 01!
WR
Experienced poster
 
Posts: 145
Joined: Thu Nov 27, 2003 9:46 am

713 why WA plz help

Postby Fuad Hassan_IIUC(DC) » Sun Apr 03, 2005 5:02 pm

why am i getteing WA :o plz help me

#include<iostream.h>
#include<stdio.h>
#include<string.h>
void strrev1(char *str1,char *str2)
{
long long len,i,j,k;
len=strlen(str1);
for(i=0;i<=len;i++)
str2[i]=str1[len-i-1];
}

int main()
{
char buffer[100000],bufferrev[100000],fuffer[100000],fufferrev[100000];
long long i,j,k,l;
while(cin>>k)
{

for(l=0;l<k;l++)
{
cin>>i>>j;
sprintf(buffer,"%lld",i);
sprintf(fuffer,"%lld",j);
strrev1(buffer,bufferrev);
strrev1(fuffer,fufferrev);
sscanf(bufferrev,"%lld",&i);
sscanf(fufferrev,"%lld",&j);
int sum=i+j;
char suffer[100000],sufferrev[100000];
sprintf(suffer,"%lld",sum);
strrev1(suffer,sufferrev);
sscanf(sufferrev,"%lld",&sum);
// cout<<i<<endl<<j<<endl;
cout<<sum<<endl;
}
}
return 0;
}
fuad
Fuad Hassan_IIUC(DC)
New poster
 
Posts: 18
Joined: Fri Jan 07, 2005 9:35 pm
Location: Bangladesh

Postby CodeMaker » Sun Apr 03, 2005 7:06 pm

The problem says, the input numbers will be almost 200 digits long. Do you think long long is going to hold 200 digit long number. definitely no. Use Big number to solve it. :-?
Jalal : AIUB SPARKS
User avatar
CodeMaker
Experienced poster
 
Posts: 183
Joined: Thu Nov 11, 2004 12:35 pm
Location: AIUB, Bangladesh

Now Run Time Error????

Postby Fuad Hassan_IIUC(DC) » Tue Apr 05, 2005 9:01 pm

now run time error!!! plz help me :o

#include<stdio.h>
#include<string.h>
#include<iostream.h>
#define L 20000000
void reverse(char *orig,char *store)
{
long long len;
len=strlen(orig);
long long i;
for(i=len-1;i>=0;i--)
{
store[len-i-1]=orig[i];
store[len]='\0';
}
}

void add(char s[], char t[], char f[])
{
char r[L],sf[L],tf[L];
int i,j,n,c,s_len,t_len,m_len,sum,s_num,t_num,M;

s_len=strlen(s);
t_len=strlen(t);

for (i=0;i<s_len;i++)
sf[i]=s[s_len-i-1];
for (i=0;i<t_len;i++)
tf[i]=t[t_len-i-1];

for(i=s_len;i<L;i++)
sf[i]='0';
for(i=t_len;i<L;i++)
tf[i]='0';

c=0;
for(i=0;i<L;i++)
{
s_num=sf[i]-48;
t_num=tf[i]-48;
sum=s_num+t_num+c;
if (sum>9)
{
n = sum%10; c=1;
}
else
{
n = sum; c=0;
}
r[i]= n+48;
}

for(i=L-1;i>=0;i--)
if (r[i]!='0') break;

M=i;
if (M==-1)
{
f[0]='0';
f[1]='\0';
}
else
{
for (i=0;i<=M;i++)
f[M-i]=r[i];
f[i]='\0';
}
}

int main()
{

char a[L],b[L],res[L],arev[L],brev[L],resrev[L];
long long i,k,l;

cin>>k;

for(l=0;l<k;l++)
{
scanf("%s%s",a,b);
reverse(a,arev);
reverse(b,brev);
add(arev,brev,res);
reverse(res,resrev);
for(i=strlen(resrev)-1;i>0;i--)
{
if(resrev[i]=='0')
{
resrev[i]='\0';
}
else break;
}
for(i=0;i<strlen(resrev);i++)
if(resrev[i]!='0') break;
for(i=i;i<strlen(resrev);i++) cout<<resrev[i];
cout<<endl;



}
return 0;
}
fuad
Fuad Hassan_IIUC(DC)
New poster
 
Posts: 18
Joined: Fri Jan 07, 2005 9:35 pm
Location: Bangladesh

Postby J&Jewel » Sat Apr 09, 2005 9:58 am

HI every one can any one who got AC give me some input/output for this problem...?I got W/A toooo. I tried too much but nothing else without W/A....Thanks....we will discuss it later.
I hate Wrong Answer!
User avatar
J&Jewel
New poster
 
Posts: 50
Joined: Thu Jul 31, 2003 10:43 am
Location: Daffodil University,Dhaka,Bangladesh

Postby Homeway » Sun May 01, 2005 6:29 pm

>____<

I got W.A. too.

I have tested the all data in this topic , but I still get W.A.
Can anyone help me about the problem ?

Code: Select all
#include <stdio.h>
#include <string.h>

class BigNum {
  private:
    enum { DigitSize = 1024 };
    char  number[DigitSize];
    int   size;
  public:
    BigNum () {
      Clean();
    }

    inline void Clean(){
      size = 0;
      for (int i = 0;i < DigitSize;i ++){
   number[i] = 0;
      }
    }

    void SetNum(char *str){
      size = strlen(str);
      for (int i = 0; i < size; i++) {
   number[i] = str[size-i-1] - '0';
      }

    }

    friend BigNum& operator+(const BigNum& a,const BigNum& b);

    void Print(){
      int i;
      int flag = 0;
      for(i = size ; i >= 0 ; i--){
   if (number[i] != 0 || flag == 1)
     printf("%d",number[i]);   
   if (number[i] != 0 )
     flag = 1;
      }
      if (flag == 0 )
   printf("0");
    }

    void Reverse(){
      char num[DigitSize];
      int i;
      for (i = 0;i < size ;i++){
   num[i] = number[size-i-1];      
      }
      for (i = 0 ; i < size ; i++){
   number[i] = num[i];
      }
    }

    void ReSize(){
      int i,sz;
      for(i = size; i >= 0;i--){
   if (number[i] != 0) break;
      }
      size = i+1;
    }
};

BigNum& operator+(const BigNum& a,const BigNum& b)  {
  int i;
  int sizeMax;
  int flag;
  static BigNum num ;

  num.Clean();
  sizeMax = (a.size >= b.size) ? a.size : b.size;

  for (i = flag = 0; i < sizeMax; i++){
    num.number[i] = a.number[i] + b.number[i] + flag ;
    if (num.number[i] >= 10){
      num.number[i] -= 10;
      flag = 1;
    } else
      flag = 0;
  }

  if (flag == 1){
    num.size = sizeMax + 1;
    num.number[i] = flag;
  }
  else
    num.size = sizeMax;

  return num;
}



int main(){
  char in1[1024],in2[1024];

  while(scanf(" %s%s",in1,in2) != EOF){
   
    BigNum aa,bb,cc;
    aa.SetNum(in1);
    bb.SetNum(in2);
    aa.ReSize();
    bb.ReSize();
    aa.Reverse();
    bb.Reverse();
    cc = aa + bb;
    cc.ReSize();
    cc.Reverse();
    cc.Print();
    printf("\n");
  }

  return 0;
}


Let Program Save My Life.
User avatar
Homeway
New poster
 
Posts: 3
Joined: Wed Sep 11, 2002 5:53 am

Postby n00i3 » Thu Sep 22, 2005 6:42 pm

i think im going to lose my mind... everythign works .. c++ code compiles fine on linux & windows platforms but our wonderful judge has issue with compiling it

:x :x :x :x :x :x :x :x :x :x :evil: :evil: :evil: :evil: :evil: :roll: :roll: :roll: :roll: :roll: :roll: :roll: :roll:

:cry: :cry: :cry: :cry: :cry: :cry: :cry:
n00i3
New poster
 
Posts: 20
Joined: Thu Sep 15, 2005 8:49 pm

PreviousNext

Return to Volume VII

Who is online

Users browsing this forum: No registered users and 1 guest