713 - Adding Reversed Numbers

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

Moderator: Board moderators

Joseph Kurniawan
Experienced poster
Posts: 136
Joined: Tue Apr 01, 2003 6:59 am
Location: Jakarta, Indonesia

Post by Joseph Kurniawan »

Wow!!!
This really scares me. So my stat of solved prob isn't true for sure? There can be probs that's acc for mere dumb luck???
Ivor
Experienced poster
Posts: 150
Joined: Wed Dec 26, 2001 2:00 am
Location: Tallinn, Estonia

Post by Ivor »

to be sure you would need to test your program with any possible input. And that could take GIGABYTES of input files. And some programs you can't test with all cases at all -> you'll run out of living time! If you got your solution accepted, it means that it is correct with high probability (99.9%? depends on how good is the input file).

Ivor
There is a theory which states that if ever anyone discovers exactly what the Universe is for and why it is here, it will instantly disappear and be replaced by something even more bizarre and inexplicable.
Joseph Kurniawan
Experienced poster
Posts: 136
Joined: Tue Apr 01, 2003 6:59 am
Location: Jakarta, Indonesia

Post by Joseph Kurniawan »

Yeah, I think you're right. After all it's just a brain exercise, nothing to lose!!! :wink: :wink:
ravingavin
New poster
Posts: 21
Joined: Sun Sep 14, 2003 11:44 pm
Location: USA
Contact:

713

Post by ravingavin »

It keeps on telling me that I have a compile error, and I have no idea what the deal is.

I'm using Visual C++, and it's giving me no errors. I know little about compilers and libraries.

I've got a feeling it's to do with me using the cstring library. If someone could notify me of what to change then I would really appreciate it.


Thanks for you help!!

GCS

Code: Select all

#include <iostream>
#include <cstring>
#include <cmath>

using namespace std;

int main(){

	char x[30];
	char y[30];
	int final[30];
	char *xcopy, *ycopy;
	int N, length1, length2, xtemp, ytemp, remember,temp, answer, modulus;

	cin >> N;

	int i=0;

	while(i < N){

		cin >> x >> y;

		length1 = strlen(x);

		int p = length1-1;
		
		xcopy = new char[length1];

		for(int t=0; t < length1; t++){
			xcopy[t] = x[p];

			p--;
		} 
		
		length2 = strlen(y);

		p = length2-1;

		ycopy = new char[length2];

		for(t=0; t < length2; t++){
			ycopy[t] = y[p];

			p--;
		} 

		xtemp = atoi(xcopy);

		ytemp = atoi(ycopy);

		xtemp += ytemp;

		t=1;

		while(pow(10,t)/xtemp < 1)
			t++;

		temp= pow(10,t-1);

		if(xtemp != temp*10){

			remember= t;

			while(t != 0){
			
				temp= pow(10,t-1);
			
				modulus = (xtemp % temp);
			
				answer= ((xtemp - modulus) / temp);

				final[t-1] = answer;

				xtemp = modulus;

				t--;
			}

			for(int b=0; b < remember; b++)
				cout << final[b];
		}
		else{
			cout << "1" << endl;
		}


		cout << endl;

		delete [] xcopy;
		delete [] ycopy;

		i++;

	}



	return 0;

}

jpfarias
Learning poster
Posts: 98
Joined: Thu Nov 01, 2001 2:00 am
Location: Brazil
Contact:

Compile errors...

Post by jpfarias »

These are the errors I've got when I compiled your code on the gnu g++ compiler with -ansi and -Wall flags:

Code: Select all

teste.cpp: In function `int main()':
teste.cpp:41: name lookup of `t' changed for new ISO `for' scoping
teste.cpp:29:   using obsolete binding at `t'
teste.cpp:55: call of overloaded `pow(int, int&)' is ambiguous
/usr/include/bits/mathcalls.h:154: candidates are: double pow(double, double)
/usr/include/c++/3.2.3/cmath:427:                 long double std::pow(long 
   double, int)
/usr/include/c++/3.2.3/cmath:423:                 float std::pow(float, int)
/usr/include/c++/3.2.3/cmath:419:                 double std::pow(double, int)
/usr/include/c++/3.2.3/cmath:410:                 long double std::pow(long 
   double, long double)
/usr/include/c++/3.2.3/cmath:401:                 float std::pow(float, float)
teste.cpp:58: call of overloaded `pow(int, int)' is ambiguous
/usr/include/bits/mathcalls.h:154: candidates are: double pow(double, double)
/usr/include/c++/3.2.3/cmath:427:                 long double std::pow(long 
   double, int)
/usr/include/c++/3.2.3/cmath:423:                 float std::pow(float, int)
/usr/include/c++/3.2.3/cmath:419:                 double std::pow(double, int)
/usr/include/c++/3.2.3/cmath:410:                 long double std::pow(long 
   double, long double)
/usr/include/c++/3.2.3/cmath:401:                 float std::pow(float, float)
teste.cpp:66: call of overloaded `pow(int, int)' is ambiguous
/usr/include/bits/mathcalls.h:154: candidates are: double pow(double, double)
/usr/include/c++/3.2.3/cmath:427:                 long double std::pow(long 
   double, int)
/usr/include/c++/3.2.3/cmath:423:                 float std::pow(float, int)
/usr/include/c++/3.2.3/cmath:419:                 double std::pow(double, int)
/usr/include/c++/3.2.3/cmath:410:                 long double std::pow(long 
   double, long double)
/usr/include/c++/3.2.3/cmath:401:                 float std::pow(float, float)
Maybe MSVC++ is not ansi compatible. Ansi is the standard used in Valladolid online judge. Look in MSVC++ options if there is a ansi compatible setting you can set and try compiling again.... Anyway, you can get Cygwin or MingW which are the gnu compilers ported to windows.

JP!
ravingavin
New poster
Posts: 21
Joined: Sun Sep 14, 2003 11:44 pm
Location: USA
Contact:

Post by ravingavin »

Thanks....I found the error! The error was that I kept on using 'i' in the for loops after using it the first time in a for loop. It obviously destroys the variable in memory once the for loop ends.

Thanks for your help!!
ravingavin
New poster
Posts: 21
Joined: Sun Sep 14, 2003 11:44 pm
Location: USA
Contact:

Post by ravingavin »

Thanks for your help again!!! Well, I thought that I had everything correct, but I've submitted it and apparently there is something wrong with my code.

Could someone give me some test cases in which my code is incorrect. I know that i'm really close, so it's probaly a minor error.

Thanks once again!

GCS

Code: Select all

#include <iostream>
#include <string>
#include <cmath>

using namespace std;

int main(){

	char x[30];
	char y[30];
	char *xcopy, *ycopy;
	int N, length1, length2, xtemp, ytemp, remember,temp, answer, modulus, *final;

	cin >> N;

	int i=0;

	while(i < N){

		cin >> x >> y;

		length1 = strlen(x);

		int p = length1-1;
		
		xcopy = new char[length1];

		for(int t=0; t < length1; t++){
			xcopy[t] = x[p];

			p--;
		} 
		
		length2 = strlen(y);

		p = length2-1;

		ycopy = new char[length2];

		for(int g=0; g < length2; g++){
			ycopy[g] = y[p];

			p--;
		} 

		xtemp = atoi(xcopy);

		ytemp = atoi(ycopy);

		xtemp += ytemp;

		int f=1;

		while(pow(10,f)/xtemp < 1)
			f++;

		temp= pow(10,f-1);

		final = new int[f];

		if(xtemp != temp*10){

			remember= f;

			while(f != 0){
			
				temp= pow(10,f-1);
			
				modulus = (xtemp % temp);
			
				answer= ((xtemp - modulus) / temp);

				final[f-1] = answer;
				
				xtemp = modulus;
				
					f--;
				
			}


			int w=0;
			
			for(int b=0; b < remember; b++){
				if(w==0 && final[b] == 0)
					continue;
				
				else if(w==0 && final[b] != 0)
					w++;

					cout << final[b];
			}

			if(w==0)
				cout << "0";
			
		}
		else{
			cout << "1" << endl;
		}


		cout << endl;

		delete [] xcopy;
		delete [] ycopy;
		delete [] final;

		i++;

	}
		


	return 0;

}
jpfarias
Learning poster
Posts: 98
Joined: Thu Nov 01, 2001 2:00 am
Location: Brazil
Contact:

Post by jpfarias »

I've found one error:

[cpp]
else{
cout << "1" << endl;
}


cout << endl;
[/cpp]

This will produce 2 endl, which is wrong....

Don't know if there are other errors.... :)

JP.
ravingavin
New poster
Posts: 21
Joined: Sun Sep 14, 2003 11:44 pm
Location: USA
Contact:

Post by ravingavin »

Thanks...I didn't notice that error. I've corrected the error by it's still giving me WA.

GCS
ravingavin
New poster
Posts: 21
Joined: Sun Sep 14, 2003 11:44 pm
Location: USA
Contact:

Input

Post by ravingavin »

Could somebody perhaps give me some good input test data, so I can see where my program is going wrong? I cannot find the problem. It keeps on giving me WA.

Thanks for your help!!

GCS
lky
New poster
Posts: 21
Joined: Fri Dec 05, 2003 5:59 pm
Contact:

713

Post by lky »

any critical data in this problems.....i tryed loads of them ....but still dunno why WA

here is my code:
program q713;
uses wincrt;
var a:array[1..300]of integer;
n,i,j,k,u,v:integer;
s,s1,s2,t1,t2:string;
check:boolean;
begin
readln(n);
for i:=1 to n do
begin
check:=false;
readln(s);
for j:=1 to 300 do
a[j]:=0;
for j:=1 to length(s) do
if s[j]=' '
then k:=j;
s1:=copy(s,1,k-1);
s2:=copy(s,k+1,length(s)-k);
for j:=1 to length(s1) do
t1[j]:=s1[length(s1)+1-j];
for j:=1 to length(s2) do
t2[j]:=s1[length(s2)+1-j];
for j:=1 to length(s2) do
a[j]:=a[j]+ord(s2[j])-48;
for j:=1 to length(s1) do
a[j]:=a[j]+ord(s1[j])-48;

if length(s2)>length(s1)
then begin
for j:=1 to length(s2) do
if a[j]>=10
then begin
a[j+1]:=a[j+1]+1;
a[j]:=a[j]-10
end;
end
else begin
for j:=1 to length(s1) do
if a[j]>=10
then begin
a[j+1]:=a[j+1]+1;
a[j]:=a[j]-10
end;
end;
for j:=300 downto 1 do
if (a[j]<>0)and(check=false)
then begin
u:=j;
check:=true
end;
check:=false;
for j:=1 to 300 do
if (a[j]<>0)and(check=false)
then begin
v:=j;
check:=true
end;
for j:=v to u do
write(a[j]);
writeln
end
end.
Morning
Experienced poster
Posts: 134
Joined: Fri Aug 01, 2003 2:18 pm
Location: Shanghai China

&#65283;713 I need test numbers to know why i got WA,pli

Post by Morning »

[java]
import java.io.*;
import java.util.*;

class Main
{
static String reverse(String str) //utility function to reverse the string
{
char [] chr = new char [str.length()];
for(int loop = str.length() - 1;loop >= 0;loop--)
{
chr[str.length() - 1 - loop] = str.charAt(loop);
}
return (new String(chr));
}
static String ReadLn (int maxLg) // utility function to read from stdin
{
byte lin[] = new byte [maxLg];
int lg = 0, car = -1;
while (lg < maxLg)
{
car = System.in.read();
if ((car < 0) || (car == '\n')) break;
lin [lg++] += car;
}
return (new String (lin, 0, lg));
}

public static void main (String args[]) // entry point from OS
{
Main myWork = new Main(); // create a dinamic instance
myWork.Begin(); // the true entry point
}

void Begin()
{
String input,revC;
StringTokenizer idata;
int times,a,b,c;
input = Main.ReadLn(255);
idata = new StringTokenizer (input);
times = Integer.parseInt (idata.nextToken());
while (times > 0)
{
times--; //count the times
input = Main.ReadLn(255);
input = new String(reverse(input));
idata = new StringTokenizer (input);
a = Integer.parseInt (idata.nextToken());
b = Integer.parseInt (idata.nextToken());
c = a + b;
revC = new String(reverse(Integer.toString(c)));
idata = new StringTokenizer (revC);
c = Integer.parseInt (idata.nextToken());
System.out.println(c);
}
}
}
[/java]
"Learning without thought is useless;thought without learning is dangerous."
"Hold what you really know and tell what you do not know -this will lead to knowledge."-Confucius
Morning
Experienced poster
Posts: 134
Joined: Fri Aug 01, 2003 2:18 pm
Location: Shanghai China

713

Post by Morning »

[java]
import java.io.*;
import java.util.*;

class Main
{
static String reverse(String str) //utility function to reverse the string
{
char [] chr = new char [str.length()];
for(int loop = str.length() - 1;loop >= 0;loop--)
{
chr[str.length() - 1 - loop] = str.charAt(loop);
}
return (new String(chr));
}
static String ReadLn (int maxLg) // utility function to read from stdin
{
byte lin[] = new byte [maxLg];
int lg = 0, car = -1;
while (lg < maxLg)
{
car = System.in.read();
if ((car < 0) || (car == '\n')) break;
lin [lg++] += car;
}
return (new String (lin, 0, lg));
}

public static void main (String args[]) // entry point from OS
{
Main myWork = new Main(); // create a dinamic instance
myWork.Begin(); // the true entry point
}

void Begin()
{
String input,revC;
StringTokenizer idata;
int times,a,b,c;
input = Main.ReadLn(255);
idata = new StringTokenizer (input);
times = Integer.parseInt (idata.nextToken());
while (times > 0)
{
times--; //count the times
input = Main.ReadLn(255);
input = new String(reverse(input));
idata = new StringTokenizer (input);
a = Integer.parseInt (idata.nextToken());
b = Integer.parseInt (idata.nextToken());
c = a + b;
revC = new String(reverse(Integer.toString(c)));
idata = new StringTokenizer (revC);
c = Integer.parseInt (idata.nextToken());
System.out.println(c);
}
}
}[/java]
"Learning without thought is useless;thought without learning is dangerous."
"Hold what you really know and tell what you do not know -this will lead to knowledge."-Confucius
sohel
Guru
Posts: 856
Joined: Thu Jan 30, 2003 5:50 am
Location: New York

Why man Why?

Post by sohel »

After rejudging I got WA too.

This problem looks straight forward and I don't know why I keep getting Wrong Answer.

I don't usually post my code(AC or WA) in this board but as the code is very short I couldn't refrain myself from doing so.

here is my code:
[cpp]
#include<stdio.h>
#include<string.h>

void srev(char val[])
{
int l,i;
char t;
l = strlen(val) - 1;
for(i=0;i<l;i++)
{
t = val;
val = val[l];
val[l] = t;
l--;
}
}
int main()
{
int in, j, i,k;
int t;
long x,y,sum;
char str1[10000], str2[10000];
scanf("%d",&t);
while(t--)
{
scanf("%ld %ld",&x, &y);
sprintf(str1,"%ld",x);
sprintf(str2,"%ld",y);
srev(str1);
srev(str2);
sscanf(str1,"%ld",&x);
sscanf(str2,"%ld",&y);
sum = x + y;
sprintf(str1,"%ld",sum);
srev(str1);
sscanf(str1,"%ld",&x);
printf("%ld\n",x);
}


return 0;
}
[/cpp]

Can someone tell me why this simple program gets WA.
Thanx.
Morning
Experienced poster
Posts: 134
Joined: Fri Aug 01, 2003 2:18 pm
Location: Shanghai China

Post by Morning »

our algorithm seems likely:)
"Learning without thought is useless;thought without learning is dangerous."
"Hold what you really know and tell what you do not know -this will lead to knowledge."-Confucius
Post Reply

Return to “Volume 7 (700-799)”