10018 - Reverse and Add

All about problems in Volume 100. 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
Juergen Werner
New poster
Posts: 27
Joined: Wed Apr 17, 2002 7:54 pm

Post by Juergen Werner » Wed Sep 18, 2002 11:29 pm

ret = (unsigned long)atol(new);

Code: Select all

# man 3 atol

ATOI(3)             Linux Programmer's Manual             ATOI(3)

NAME
       atoi,  atol, atoll, atoq - convert a string to an integer.

SYNOPSIS
       #include <stdlib.h>

       int atoi(const char *nptr);
       long atol(const char *nptr);
       long long atoll(const char *nptr);
       long long atoq(const char *nptr);
The atol function returns long, so casting to unsigned long afterwards won"t help if the result did not fit into long. Changing to long long and using atoll would maybe help, but I'd rather use sscanf.

Red Scorpion
Experienced poster
Posts: 192
Joined: Sat Nov 30, 2002 5:14 am

10018

Post by Red Scorpion » Thu Dec 12, 2002 7:12 am

[color=red][/color][size=18][/size]Hi,
I have solved this problem(Reverse and Add) a month ago, and it got accepted.
Now, I looks in the judge status, and my list of problem(10018) become dissappear, then I send again that problem, and get Wrong Answer?

If anyone have time , please help me. This is my code (c++):

#include <stdio.h>
#include <string.h>
#define MAX 2000
#define FALSE 0
#define TRUE 1

void swap(char *dest, char *source) { //parameter : destination, source
int i,len;

len = strlen(source);

for (i=len-1; i>=0; i--)
dest[len-i-1] = source[i];
dest[len] = '\x0';
}

void sum(char *temp, char *str, char *result) {
int i, len, stor = 0, value;

len = strlen(temp);

for (i=0; i<len; i++) {
value = temp[i]-48 + str[i]-48 + stor;
if (value>9) {
stor = 1;
result[i] = (value % 10) + 48;
}
else {
stor = 0;
result[i] = value + 48;
}
}

if (stor) {
result[i] = '1';
result[i+1] = '\x0';
}
else result[i] = '\x0';

strcpy(temp,result);
swap(str, temp); //destination . source
}

void process(char *str, char *result) {
int cloop=0, //count loop
found = FALSE;
char temp[MAX];

swap (temp, str); //destination, source

do {
if (!strcmp(temp,str)) {
printf ("%d %s\n", cloop, str);
found = TRUE;
}
else {
cloop++;
sum(temp, str, result);
}
}while (found!=TRUE);
}

int main () {
int n,i;
char str[MAX],
result[MAX];

// scanf ("%d\n", &n);
fgets(str,MAX,stdin);
sscanf (str, "%d", &n);

for (i=1; i<=n; i++) {
fgets(str,MAX,stdin);
if (str[strlen(str)-1] == '\n')
str[strlen(str)-1] = '\x0';

process(str, result);
}
return 0;
}
[/code][cpp][/img][/cpp][/code][/url][/quote]

Adrian Kuegel
Guru
Posts: 724
Joined: Wed Dec 19, 2001 2:00 am
Location: Germany

Post by Adrian Kuegel » Thu Dec 12, 2002 11:28 pm

Try this testcase:
Input:
2
2
99
Output:
1 4
6 79497

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

Post by Dominik Michniewski » Fri Dec 13, 2002 9:32 am

But in first case 2 is a palindrome without any computing .... ?
So output shouldn't be "0 2" ??

Dominik

Adrian Kuegel
Guru
Posts: 724
Joined: Wed Dec 19, 2001 2:00 am
Location: Germany

Post by Adrian Kuegel » Fri Dec 13, 2002 5:30 pm

That was what I thought first, too. But read the description careful. It says after adding the reversed number you look the first time if the result is a palindrome.

Red Scorpion
Experienced poster
Posts: 192
Joined: Sat Nov 30, 2002 5:14 am

Problem 10018

Post by Red Scorpion » Sat Dec 14, 2002 4:16 am

The judge is error when rejudged problem 10018 and now the judged has fixed it and received my problem.

Thanks. :roll:

off_algos
New poster
Posts: 29
Joined: Wed Nov 13, 2002 11:37 am
Location: india

10018...reverse and add..WA!!!

Post by off_algos » Mon Dec 16, 2002 3:14 pm

Code: Select all

#include <stdio.h>

int ispal(int m)
{
    int n=m;
    int p=0;
    while(n)
    {
	p=p*10+n%10;
	n/=10;
    }
    if(p==m)
	return -1;
    return p;
}

int main()
{
    int n;
    int p,count;
    scanf("%d",&n);
    while(n--)
    {
	count=0;
	scanf("%d",&p);
	while(1)
	{
	    int q=ispal(p);
	    if(q<0)
	    {
		break;
	    }
	    p+=q;
	    count++;
	}
	printf("%d %d\n",count,p);
    }
    return 0;
}
[b]
this code was accepted before and later judge changed his mind to WA
somebody please help me..[/b]

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

Post by Dominik Michniewski » Mon Dec 16, 2002 3:25 pm

try this:
input:
2
output:
1 4

Regards
Dominik

PS. This is small misunderstanding of problem description ... I made same error before rejudgement ...
AND PLEASE READ OTHER POSTS BEFORE YOU CREATE NEW ONE!!!!
This should keep this place in order ...

off_algos
New poster
Posts: 29
Joined: Wed Nov 13, 2002 11:37 am
Location: india

same again

Post by off_algos » Mon Dec 16, 2002 3:40 pm

Code: Select all

#include <stdio.h>

int ispal(int m)
{
    int n=m;
    int p=0;
    while(n)
    {
	p=p*10+n%10;
	n/=10;
    }
    if(p==m)
	return -1;
    return p;
}

int main()
{
    int n;
    int p,count;
    scanf("%d",&n);
    while(n--)
    {
	count=0;
	scanf("%d",&p);
	while(1)
	{
	    int q=ispal(p);
	    if(q<0)
	    {
		if(count)
		    break;
	    }
	    if(q>0)
		p+=q;
	    else
		p*=2;
	    count++;
	}
	printf("%d %d\n",count,p);
    }
    return 0;
}
this new code gives me answers for pals and yet i get WA.

FlyDeath
Learning poster
Posts: 73
Joined: Wed Jan 02, 2002 2:00 am
Location: Taiwan

Post by FlyDeath » Wed Dec 18, 2002 3:29 pm

I suggest you use long or long lon instead of int.

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

Post by Dominik Michniewski » Wed Dec 18, 2002 4:26 pm

On OJ system it's the same: long and int ..... Both variable are 32bit ....
But I agree with you - int should be smaller than long int :) and there was earlier .... int had 16 bits, long int 32 .... So we should use long int instead of int ...

Regards
Dominik

Taslim
New poster
Posts: 9
Joined: Sun Dec 08, 2002 11:47 am
Contact:

10018 a funny problem....

Post by Taslim » Wed Dec 18, 2002 11:23 pm

At the time of regug its giving WA ....Whats wrong with my program..do u know?


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void main()
{
char in[12],in1[12];
long i,k,n,num1,num2,c;
while(scanf("%ld",&n)!=EOF)
{
for(k=0;k<n;k++)
{
scanf("%s",in);
strcpy(in1,in);
int len=strlen(in),j=0;
for (i=len-1;i>=0;i--)
{
in1[j]=in; j++;
}
c=0;
{
do{ c++;
num1=atol(in);
num2=atol(in1);
num1+=num2;

sprintf(in,"%ld",num1);
sprintf(in1,"%ld",num1);
strcpy(in1,in);
len=strlen(in);j=0;
for (i=len-1;i>=0;i--)
{
in1[j]=in; j++;
}

}while(strcmp(in,in1)!=0);

printf("%ld %ld\n",c,num1);
}
}
}
}

:-?
THanks...
http://www.softhome1.netfirms.com[/c]

Taslim
New poster
Posts: 9
Joined: Sun Dec 08, 2002 11:47 am
Contact:

10018 Why WA can u help me?

Post by Taslim » Fri Dec 20, 2002 5:41 pm

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void main()
{
char in[12],in1[12];
long i,k,n,num1,num2,c;
while(scanf("%ld",&n)!=EOF)
{
for(k=0;k<n;k++)
{
scanf("%s",in);
strcpy(in1,in);
int len=strlen(in),j=0;
for (i=len-1;i>=0;i--)
{
in1[j]=in; j++;
}
c=0;
{
do{ c++;
num1=atol(in);
num2=atol(in1);
num1+=num2;

sprintf(in,"%ld",num1);
sprintf(in1,"%ld",num1);
strcpy(in1,in);
len=strlen(in);j=0;
for (i=len-1;i>=0;i--)
{
in1[j]=in; j++;
}

}while(strcmp(in,in1)!=0);

printf("%ld %ld\n",c,num1);
}
}
}
}



:(

popel
New poster
Posts: 33
Joined: Fri Mar 15, 2002 2:00 am
Location: Dhaka, Bangladesh
Contact:

Post by popel » Sun Dec 22, 2002 12:24 pm

Adrian Kuegel wrote:Try this testcase:
Input:
2
2
99
Output:
1 4
6 79497
.....Checking problems once accepted is a boring task.
...But what about me? My code yields the same output you presented.

Here is my one:
[c]
#include<stdio.h>
unsigned reverse(unsigned n);
unsigned reverse(unsigned n){
unsigned len,temp,j,k,rev;
len=0;temp=n;rev=0;
while(temp){len++;temp/=10;}
for(j=len;j>=1;j--){
k=1;
for(temp=1;temp<j;temp++)k*=10;
rev+=k*(n%10);
n/=10;
}
return(rev);

}

void main(){
unsigned N,i,j,rev;
scanf("%d",&N);
for(i=0;i<N;i++){
scanf("%d",&rev);
j=0;
do{
j++;
rev+=reverse(rev);

}while(rev!=reverse(rev));
printf("%d %d\n",j,rev);
}
}
[/c]

______________________________________________Md.Tanvir Al Amin

Larry
Guru
Posts: 647
Joined: Wed Jun 26, 2002 10:12 pm
Location: Hong Kong and New York City
Contact:

Post by Larry » Sun Dec 22, 2002 6:57 pm

Once again, use long long...

Code: Select all

4000000000
should yield

Code: Select all

1 4000000004

Post Reply

Return to “Volume 100 (10000-10099)”