Page 1 of 12

Posted: Wed Sep 18, 2002 11:29 pm
by Juergen Werner
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.

10018

Posted: Thu Dec 12, 2002 7:12 am
by Red Scorpion
[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]

Posted: Thu Dec 12, 2002 11:28 pm
by Adrian Kuegel
Try this testcase:
Input:
2
2
99
Output:
1 4
6 79497

Posted: Fri Dec 13, 2002 9:32 am
by Dominik Michniewski
But in first case 2 is a palindrome without any computing .... ?
So output shouldn't be "0 2" ??

Dominik

Posted: Fri Dec 13, 2002 5:30 pm
by Adrian Kuegel
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.

Problem 10018

Posted: Sat Dec 14, 2002 4:16 am
by Red Scorpion
The judge is error when rejudged problem 10018 and now the judged has fixed it and received my problem.

Thanks. :roll:

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

Posted: Mon Dec 16, 2002 3:14 pm
by off_algos

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]

Posted: Mon Dec 16, 2002 3:25 pm
by Dominik Michniewski
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 ...

same again

Posted: Mon Dec 16, 2002 3:40 pm
by off_algos

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.

Posted: Wed Dec 18, 2002 3:29 pm
by FlyDeath
I suggest you use long or long lon instead of int.

Posted: Wed Dec 18, 2002 4:26 pm
by Dominik Michniewski
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

10018 a funny problem....

Posted: Wed Dec 18, 2002 11:23 pm
by Taslim
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]

10018 Why WA can u help me?

Posted: Fri Dec 20, 2002 5:41 pm
by Taslim
#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);
}
}
}
}



:(

Posted: Sun Dec 22, 2002 12:24 pm
by popel
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

Posted: Sun Dec 22, 2002 6:57 pm
by Larry
Once again, use long long...

Code: Select all

4000000000
should yield

Code: Select all

1 4000000004