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

Post Reply
FT
New poster
Posts: 2
Joined: Tue Jun 25, 2002 8:21 am

713 - Adding Reversed Numbers

Post by FT » Wed Jul 03, 2002 8:42 am

Anyone can give me some test datas.
My program got Wrong answer,
and i don't know what is wrong with it.

Code: Select all

#include<iostream.h>
#include<string.h>
void main()
{
  int N;
  int i,len1,len2,advance,temp,flag;
  char num1[1000];
  char num2[1000];
  cin>>N;
  while(N--){
     cin>>num1;
	 cin>>num2;
	 len1=strlen(num1);
	 len2=strlen(num2);
	 i=0;
	 advance=0;
	 flag=0;
	 while(i<len1&&i<len2){
	    temp=(num1[i]-'0')+(num2[i]-'0')+advance;
		if(temp%10) flag=1;
		if(flag)  cout<<temp%10;
		advance=temp/10;
		i++;
	 }
	 if(i>=len1){
		 while(i<len2){
		    temp=advance+(num2[i]-'0');
			if(temp%10) flag=1;
			if(flag)  cout<<temp%10;
			advance=temp/10;
			i++;
		 }
	 }else{
	     while(i<len1){
		    temp=advance+(num1[i]-'0');
			if(temp%10) flag=1;
			if(flag) cout<<temp%10;
			advance=temp/10;
			i++;
		 }
	 }
	 if(advance) cout<<advance;
	 cout<<endl;
  }
}[cpp][/cpp]

Caesum
Experienced poster
Posts: 225
Joined: Fri May 03, 2002 12:14 am
Location: UK
Contact:

Post by Caesum » Wed Jul 03, 2002 6:49 pm

Input:
30 30
Output:
6

henar2
New poster
Posts: 30
Joined: Mon Nov 26, 2001 2:00 am
Location: Valladolid (Spain)

Post by henar2 » Mon Jul 22, 2002 4:02 pm

Hi.
I am getting Wrong Answer and I don't know why, please check it:

[c]
#include<stdio.h>
#include<stdlib.h>

void invert(int *num);

main()
{
int cases,i,num1,num2,sum;
scanf("%d",&cases);
for(i=0;i<cases;i++)
{
scanf("%d %d",&num1,&num2);
invert(&num1);
invert(&num2);
sum=num1+num2;
invert(&sum);
printf("%d\n",sum);
}
}


void invert(int *num)
{
int i,n;
char string[50],string2[50],*end;
sprintf(string,"%d",*num);
n=strlen(string);
for(i=0;i<n;i++)
string2=string[n-i-1];
string2[n]='\0';
*num=strtol(string2,&end,0);
return;
}
[/c]

liusu
New poster
Posts: 22
Joined: Thu Aug 01, 2002 10:26 am

713

Post by liusu » Thu Aug 01, 2002 10:32 am

Why WA?I can't understand.why?Give me a reason!

:cry: [pascal]var
s1,s2:array[1..20] of integer;
str:string;
i,t,j,tt:integer;
total:longint;
tem:integer;
begin
readln(total);
while (total>0) do
begin
readln(str);
for i:=1 to 20 do
begin
s2:=0;
s1:=0;
end;

i:=1;
t:=1;
while ((str[t]=' ')or(str[t]='0')) and(t<=length(str)) do
inc(t);
while not (str[t]=' ') and(t<=length(str))do
begin
s1:=ord(str[t])-48;
inc(i);
inc(t);
end;
dec(i);
j:=0;
while ((str[t]=' ')or(str[t]='0'))and (t<=length(str)) do
inc(t);
while (t<=length(str)) do
begin
inc(j);
s2[j]:=ord(str[t])-48;
inc(t);

end;
tem:=i;
if(i<j) then tem:=j;
for t:=1 to tem do
begin
s1[t]:=s1[t]+s2[t];
if(s1[t]>9) then
begin
s1[t]:=s1[t]-10;
inc(s1[t+1]);
end;
end;
t:=1;

tt:=tem+1;
while(s1[tt]=0)do
dec(tt);
while (s1[t]=0) and (t<=tem)do
inc(t);
if(t>tem) then
writeln(s1[t])
else begin
for t:=t to tt do
write(s1[t]);
// inc(t);
if(s1[t]<>0)
then write(s1[t]);
writeln;
end;


dec(total);

end;
end.[/pascal]
[/pascal]

monika
New poster
Posts: 13
Joined: Tue Jul 23, 2002 9:45 am

Post by monika » Wed Oct 30, 2002 8:55 am

ur invert() function is creating trouble in case an input number ends in 0.

when string2 has leading zeroes, strtol returns 0.

eureka
New poster
Posts: 5
Joined: Sun Sep 15, 2002 11:15 am

713

Post by eureka » Sun Nov 10, 2002 11:53 am

who can tell me why my code is compile error?
what should be attention when programming with c
because I use turbo c
but judge is gcc


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

void rever_str(char *s)
{

int len = strlen(s);
int i,j;
j = 0;
int tem ;
tem = len ;
while (s[len-j-1] == '0')
{
j++;
tem --;
}

s[tem] = '\0';

int t ;
t = tem/2;
char temp;

for (i = 0;i<=t-1;i++)
{
temp = s;
s = s[tem-i-1];
s[tem-i-1] = temp;
}

}


void main()
{
char s[10],s1[10],s2[10];

int in_num,i;

scanf("%d",&in_num);


for (i = 1;i<=in_num;i++)
{
scanf("%s %s",s1,s2);
rever_str(s1);
rever_str(s2);
int n1 ;
n1 = atoi(s1);
int n2;
n2 = atoi(s2);
int n;
n = n1 + n2;
itoa(n,s,10);
rever_str(s);
printf("%s\n",s);
}

}

User avatar
kmhasan
Problemsetter
Posts: 107
Joined: Fri Oct 26, 2001 2:00 am
Location: Canada
Contact:

there is no itoa in gcc

Post by kmhasan » Sun Nov 10, 2002 5:13 pm

itoa is not a ANSI compatible function. You can consult Turbo C/C++ help to know if a functions is compatible in unix platform.

The same thing can be done using the sprintf function of stdio.h

For example:
to print the value of an integer to a string you can use:
[c]
int x = 1234;
char str[1024];
sprintf(str,"%d",x);
[/c]

eureka
New poster
Posts: 5
Joined: Sun Sep 15, 2002 11:15 am

have another error

Post by eureka » Fri Nov 15, 2002 5:27 am

thank you kmhasan

but what the following error messege mean?

01238360_24.c: In function `rever_str':
01238360_24.c:17: parse error before `int'
01238360_24.c:18: `j' undeclared (first use in this function)
01238360_24.c:18: (Each undeclared identifier is reported only once
01238360_24.c:18: for each function it appears in.)

espeicial "parse error before 'int'
[/b]

Dominik Michniewski
Guru
Posts: 834
Joined: Wed May 29, 2002 4:11 pm
Location: Wroclaw, Poland
Contact:

Post by Dominik Michniewski » Fri Nov 15, 2002 1:32 pm

Code: Select all

int len = strlen(s); 
int i,j; 
j = 0; 
int tem ; 
this fragment of code is possible only in C++, but not in C. deklaration of TEM must be before j=0; .... (As I Think...)
OJ compiler is very strict - use ONLY ANSI code structure. Every non-standard expression cuases error ... :(

Best regards

eureka
New poster
Posts: 5
Joined: Sun Sep 15, 2002 11:15 am

really?

Post by eureka » Sat Nov 16, 2002 3:45 pm

I submit my code with c++ ,and then "acc"
it's so strang!

wombat80
New poster
Posts: 1
Joined: Mon Nov 25, 2002 5:05 am

Post by wombat80 » Mon Nov 25, 2002 5:14 am

In a block in C you have to declare all the types at the begining.
So the fragment:
[c]int len = strlen(s);
int i,j;
j = 0;
int tem ; [/c]
would give an error on the second line because it is not expecting type information once the code has begun. However, in C++ you can declase types anywhere. This is important in C++, because constructors are called where the variable is defined.

route
New poster
Posts: 39
Joined: Sat Dec 21, 2002 1:25 am

713 input limit

Post by route » Wed Jan 15, 2003 4:51 pm

can the Inputs of 713 be greater than 2 ^ 31 -1 ?

Andrey Mokhov
Experienced poster
Posts: 128
Joined: Fri Nov 15, 2002 7:45 am
Location: Kyrgyzstan

Post by Andrey Mokhov » Sat Jan 18, 2003 6:47 am

No, my program reads data with scanf("%d",&n)

Good luck!
Andrey.

Master
Learning poster
Posts: 82
Joined: Thu Oct 10, 2002 1:15 pm
Location: St. Johns, Canada
Contact:

713 - Incorrect output with Accepted code !!!!! Again

Post by Master » Wed Aug 13, 2003 6:39 am

I have solved Adding Reverse Number (713) long time ago. Now I am developing a tutorial website for the beginners. For this, I am analyzing my accepted code and found a very interesting bug on my code. When I give the input as the following I found a answer which is wrong (I think) with my accepted code.

INPUT
2
3224 5873
5873 3224

OUTPUT
88
88

Here is my code.
[cpp]
Cut....
[/cpp]
I am surprised how I got accepted with this code.


M H Rasel
CUET - Old Sailor
Last edited by Master on Mon Jan 05, 2004 6:29 am, edited 1 time in total.

User avatar
UFP2161
A great helper
Posts: 277
Joined: Mon Jul 21, 2003 7:49 pm
Contact:

Post by UFP2161 » Wed Aug 13, 2003 7:05 am

Yeah, the correct output should be:
8008
8008
I guess you were just lucky that the Judge didn't have any inputs that tested whether or not you printed '0's in the middle of the string. It probably only checked the fringe cases where the resulting number might have had trailing zeroes or what not and other such things. Maybe they'll fix this now, and rejudge this problem =)

Post Reply

Return to “Volume 7 (700-799)”