Page 5 of 10

Now Run Time Error????

Posted: Tue Apr 05, 2005 9:01 pm
by Fuad Hassan_IIUC(DC)
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;
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=s[s_len-i-1];
for (i=0;i<t_len;i++)
tf=t[t_len-i-1];

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

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

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

M=i;
if (M==-1)
{
f[0]='0';
f[1]='\0';
}
else
{
for (i=0;i<=M;i++)
f[M-i]=r;
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;
}

Posted: Sat Apr 09, 2005 9:58 am
by J&Jewel
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.

Posted: Sun May 01, 2005 6:29 pm
by Homeway
>____<

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;
}



Posted: Thu Sep 22, 2005 6:42 pm
by n00i3
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:

713 - compiles fine ; but not on the judge :( - fixed :)

Posted: Thu Sep 22, 2005 7:11 pm
by n00i3
HELp!

Code: Select all

//#include <stack> 
#include <iostream>
#include <string>

using namespace std;

void swap (char *a, char *b)
{
 *a = *a + *b;
 *b = *a - *b;
 *a = *a - *b;
}

int getNumber (char c)
{
 return (int)(c-48);	
}

char getChar (int i)
{
 return (char)(i+48);	 
}


/* reverse does what it does, dont ask it to do more :p */
void reverse(string &x)
{
 // using stacks to reverse a string is freaking brilliant IMHO :)
 // too bad we cant use em :S
 
 /*
 stack<char> s;

 const int n = x.length();

 for (int i=0; i<n; ++i) 
 	 s.push(x[i]);

 for (int i=0; !s.empty(); ++i, s.pop())
 	 x[i]=s.top();
 */
 
 int n = x.length();
 
 for (int i=0 ; i < x.length()/2 ; i++) {
 	 swap(&x[i], &x[--n]);	 
 }
 
} // end reverse()



void kickTrailingZeros(string &x)
{
 int n = x.length();
 
 for (int i=n-1 ; i>0 ; i--) {
 	 if (x[i] == '0') {
	 	x.erase(i);		  
     } else
	    break;	 
 }	 
} // end kickTrailingZeros()





void kickLeadingZeros(string &x)
{ 
 while ( (x[0] == '0') && (x.length() != 1) ) {
 	   x.erase(0, 1);	   	   
 }
} // end kickLeadingZeros()





string sum( string &top, string &bottom )
{
 int nb = bottom.length();
 int nt = top.length() - 1;
	
 int a,b,c, carry = 0;
 
 //cout << top << " " << bottom << endl;
	
 for (int i=nb-1 ; i>=0 ; i--) {
	 a = getNumber(bottom[i]);
	 b = getNumber(top[nt]);
	 c = (a+b+carry) % 10;
	 
	 if ((a+b+carry) > 9)	   
	    carry = 1;
	 else
 	    carry = 0;
		
	 //cout << chr << endl;	
	 top.replace( nt, 1, 1, getChar(c) );
	 nt--;
	 //cout << top << endl;
 }
 
 if (carry == 1) {
 	// doing a bit shift here :)
 	
 	if (nt < 0) {
  	   top.insert( 0, 1, getChar(1) );
    } else if (nt == 0) {
	   b = getNumber(top[nt]);
	   c = (carry+b) % 10 ;
	   top.replace( nt, 1, 1, getChar(c) );
	   
	   if ((carry+b) > 9)
	   	  top.insert( 0, 1, getChar(1) );
	   
    } else {
	   b = getNumber(top[nt]);
	   c = carry+b;
   	   top.replace( nt, 1, 1, getChar(c) );  	   
    }
 }	 
 
 return top;	 
} // end sum()




string process( string &num1, string &num2 )
{
 reverse(num1) ; kickLeadingZeros(num1);
 reverse(num2) ; kickLeadingZeros(num2);

 string res;
 
 if (num1.length() > num2.length())
 	res = sum(num1, num2); 	
 else
    res = sum(num2, num1);
 
 reverse(res); kickLeadingZeros(res);
 
 return res;
} // end rev()





int main() 
{
 
 unsigned int cases;
 cin >> cases;
 
 string num1[cases], num2[cases];
 
 for (int i=0 ; i<cases ; i++) { 	   
  	 cin >> num1[i] >> num2[i];
 }
 
 cout << endl;
 
 for (int i=0 ; i<cases ; i++) { 	   
 	 string cpy1 = num1[i], cpy2 = num2[i]; 	 
 	 cout << process (cpy1, cpy2) << endl;
 }
 return 0;
 
} // end main(); 

Re: 713 - compiles fine everywhere ; but not on the judge :(

Posted: Fri Sep 23, 2005 1:57 am
by Martin Macko
Maybe, this will help you:

Code: Select all

foo.cc: In function `int main()':
foo.cc:159: error: ISO C++ forbids variable-size array `num1'
foo.cc:159: error: ISO C++ forbids variable-size array `num2'

Re: 713 - compiles fine everywhere ; but not on the judge :(

Posted: Fri Sep 23, 2005 4:33 am
by n00i3
Martin Macko wrote:Maybe, this will help you:

Code: Select all

foo.cc: In function `int main()':
foo.cc:159: error: ISO C++ forbids variable-size array `num1'
foo.cc:159: error: ISO C++ forbids variable-size array `num2'
hey :)

I switched it to

Code: Select all

string num1[100], num2[100];
and it still wont compile :cry: :cry: :cry: (well it compiles everywhere but @ the judge)

Re: 713 - compiles fine everywhere ; but not on the judge :(

Posted: Fri Sep 23, 2005 4:25 pm
by Project
Hi, n00i3.

I think you should try next trick:

firstly try to submit first half of your functions. If you get WA, try to submit second half of all functions.

With functions set where was Compilation Error try to do the same. And so on.

Attention: You should rebuild some functions so they would work without others...

The purpose of this trick is to catch where your code is getting CE.

Best regards!

Posted: Fri Sep 23, 2005 4:38 pm
by chunyi81
What n00i3 said is not the compile error in you code.

I compiled your code using g++ 3.3.2 and it compiles all right. Using g++ 2.95 which is the UVA OJ environment showed the error.

The insert function you used from the string class is the source of the error. For the insert function, you passed in two integers and a char. There is nothing wrong with that but a search of the manual for the string class shows that there are several insert functions and the one you used takes two size_type variables and a char.

I suggest you cast the two integers you passed into the insert function to unsigned int or unsigned. That should remove the error in your code.

Posted: Fri Sep 23, 2005 7:39 pm
by n00i3
YUp that was it :)

Thanks chunyi81, I also know now that it uses gcc 2.95! lol

thanks again mate!

Re: 713 - compiles fine everywhere ; but not on the judge :(

Posted: Fri Sep 23, 2005 7:39 pm
by n00i3
Project wrote:Hi, n00i3.

I think you should try next trick:

firstly try to submit first half of your functions. If you get WA, try to submit second half of all functions.

With functions set where was Compilation Error try to do the same. And so on.

Attention: You should rebuild some functions so they would work without others...

The purpose of this trick is to catch where your code is getting CE.

Best regards!
Thats a neat trick too :)

cheers man!

Posted: Fri Sep 23, 2005 10:30 pm
by n00i3
alright the stupid judge uses gcc 2.95 :x :x

fixed now :p

Posted: Fri Aug 11, 2006 10:58 pm
by mhayter1
Input:

100 200

999999999999999 1

00010 9

1 9

1 9

5 5

456 654

0 0

Output:

3

1

9001

1

1

1

111

0

very easy

Posted: Thu Aug 24, 2006 5:48 pm
by mak(cse_DU)
it is not so difficult;
adding reverse but no need to reverse;
add two string from left side to right side normal
print then canceling first 0 digit
input:-

101
99
out put -
2
[/code][/b][/list][/list]

713-WA- help! help!! help!!!

Posted: Thu Oct 12, 2006 7:42 pm
by nahid
:roll:
here im having WA. but for all the inputs i tryed i got right output pls help. here is my code.

-------------------------------------------------------------------------------------

Code: Select all

#include<stdio.h>
long int power(long int b,long int p);
long int revint(long int a);
void main()
{
	long int t,a,b,i,fa,fb,fab,ffab;
	while(scanf("%ld",&t)!=EOF)
	{
	for(i=1;i<=t;i++)
	{
		scanf("%ld%ld",&a,&b);
		fa=revint(a);
		fb=revint(b);
		fab=fa+fb;
		ffab=revint(fab);
		printf("%ld\n",ffab);
	}
	}
}

long int power(long int b,long int p)
{
	long int i,result=1;
	for(i=1;i<=p;i++)
		result*=b;
	return(result);
}


long int revint(long int a)
{
	long int i,mod,m[50],j,fa=0,k;
		for(i=1;a!=0;i++)
		{
			mod=a%10;
			m[i]=mod;
			a=a/10;
		}
		for(j=i-1,k=0;j>=1;j--,k++)
			fa=fa+m[j]*power(10,k);
	return(fa);
}