Page 9 of 12
Re: 10018 - Reverse and Add
Posted: Tue Dec 02, 2008 11:31 am
by Articuno
Well hi abid. Your code is alright. But you have done a little mistake. In your code you forgot a simple thing.
Have a close look:
Set the flag to 0.
You will get Ac.
Wish you good luck.

Re: 10018 - Reverse and Add
Posted: Tue Dec 02, 2008 6:31 pm
by abid_iut
thankx Articuno
U r a great helper
pls continue helping me like this

Re: 10018 - Reverse and Add
Posted: Tue Mar 17, 2009 9:17 pm
by lucasbls1
I don't know what's wrong with my code,
please somebody help me..
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void str_reverse(char* str)
{
char ch;
int i, j;
for(i = 0, j = strlen(str) - 1; i < j; ++i, --j)
{
ch = str;
str = str[j];
str[j] = ch;
}
}
main()
{
int qtde;
int i=0;
unsigned long num_original, num_invertido, num_resultado, teste;
char str[15];
char str_copia[15];
char* str_inutil[15];
char res_backup[1];
int cont;
scanf("%d\n", & qtde);
for(i=0; i< qtde; i++)
{
cont=1;
scanf("%s\n", str);
while(1)
{
str_copia[0] = '\0';
res_backup[0] = '\0';
strcpy(str_copia, str);
str_reverse(str);
num_original = strtoul(str_copia, str_inutil, 10);
num_invertido = strtoul(str, str_inutil, 10);
num_resultado = num_original + num_invertido;
char* res;
res = (char*) malloc(sizeof(char)*11);
res[0] = '\0';
str_inutil[0] = '\0';
sprintf(res, "%lu", num_resultado);
strcpy(res_backup, res);
str_reverse(res);
if(strcmp(res_backup, res)==0)
{
printf("%d %lu", cont, num_resultado);
if(i!=qtde-1)
printf("\n");
break;
}
else
{
cont++;
str[0] = '\0';
strcpy(str, res_backup);
}
}
}
}
Re:
Posted: Wed Mar 18, 2009 1:48 am
by sefakilic
chunyi81 wrote:Read the problem description:
You might assume that all tests data on this problem:
- will have an answer ,
- will be computable with less than 1000 iterations (additions),
- will yield a palindrome that is not greater than 4,294,967,295.
long is not enough for this problem. Use unsigned long, unsigned int or long long. The reason of the TLE of your code is most likely integer overflow.
Hello,
I got AC, but I am still wondering.
In problem description, it says that resulting palindrome <= 4,294,967,295.
So, it means that all numbers before the resulting palindrome is less than 4,294,967,295.
So, why unsigned int is not enough?
my compiler gcc is 4.2.4. and sizeof(unsigned int) is 4 byte. So 2^32 < 4,294,967,295.
10018 TE why?
Posted: Thu Jul 29, 2010 9:05 am
by @mjad
hi i am trying to solve it but it is TE.
help me, about it please..........
Re: 10018 TE why?
Posted: Thu Jul 29, 2010 8:47 pm
by sohel
Use the search option at the top right corner to look for existing discussions related to your problem.
Don't create a new thread - make your post in an existing one!
Re: 10018 TE why?
Posted: Sat Jul 31, 2010 7:43 am
by @mjad
thanks to reply
finally i got AC
Re: 10018 TE why?
Posted: Fri Sep 24, 2010 8:21 pm
by Martuza_iu
Why I get time limit exit? pls help me.
Code: Select all
#include<stdio.h>
int main()
{
long long int nm,rm[10000]={0},i,c,rv,b,d,j,m;
scanf("%ll",&m);
for(j=0;j<m;j++)
{
scanf("%ll",&nm);
d=-1;
b=0;
while(b!=rv)
{
d=d+1;
b=nm;
c=0;
for(i=0;nm!=0;i++)
{
rm[i]=nm%10;
nm=nm/10;
c=c+1;
}
rv=0;
for(i=0;i<c;i++)
rv=rv*10+rm[i];
nm=b+rv;
}
printf("%ll %ll\n",d,rv);
}
return 0;
}
Re: 10018 - Reverse and Add
Posted: Sun May 15, 2011 10:29 am
by tanvirfromhell
Why i got WA?
Code: Select all
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a,rs=0,r,i,fs,b,n,count;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d",&a);
rs=0;
count=0;
while(a!=0)
{
b=a;
rs=0;
while(a!=0)
{
r=a%10;
rs=rs*10+r;
a=a/10;
}
fs=b+rs;
a=fs;
if( b == rs )
break;
count++;
}
printf("%d %d\n",count,rs);
}
return 0;
}
Re: 10018 - Reverse and Add
Posted: Wed May 25, 2011 4:21 am
by aansu
can someone tell me why this gets a WA?
Code: Select all
using namespace std;
#include<iostream>
#define LLU long long unsigned
LLU rev(LLU x)
{
LLU i=0;
while (x!=0)
{
i=i*10+(x%10);
x/=10;
}
return i;
}
bool ispalin(LLU x)
{
int NO[20],i=0;
while (x!=0)
{
NO[i++]=x%10;
x/=10;
}
i--;
for (int j=0;j<i;j++,i--)
if(NO[i]!=NO[j]) return false;
return true;
}
int main()
{
int cases,inp,cnt;
cin>>cases;
while (cases--)
{
cin>>inp;
cnt=0;
while (!ispalin(inp))
{
inp=inp+rev(inp);
cnt++;
}
cout<<cnt<<" "<<inp<<endl;
}
return 0;
}
Re: 10018 - Reverse and Add
Posted: Fri May 27, 2011 11:42 am
by Munna_csedu
#include<stdio.h>
int count;
long long reverse(long long);
long long palindrome(long long n)
{
long long rev,p,m;
m=n;
rev=0;
while(n!=0)
{
p=n%10;
n=n/10;
rev=10*rev+p;
}
if(m==rev)
return rev;
else
return reverse(rev);
}
long long reverse(long long n)
{
long long rev,p,m;
m=n;
rev=0;
while(n!=0)
{
p=n%10;
n=n/10;
rev=10*rev+p;
}
count++;
return palindrome(rev+m);
}
int main()
{
long long k,t,n,pal;
scanf("%lld",&t);
for(k=1;k<=t;k++)
{
scanf("%lld",&n);
pal=palindrome(reverse(n));
printf("%d %lld\n",count,pal);
count=0;
}
return 0;
}
what is my problem??
i cant think anything
pls help meeeeeeeeeeee
10018-Reverse and Add WA?
Posted: Sat Dec 17, 2011 8:08 am
by tzupengwang
Re: 10018-Reverse and Add WA?
Posted: Tue Jan 10, 2012 2:53 am
by brianfry713
a is a long long int and should be printed with format code %lld
Re: 10018-Reverse and Add WA?
Posted: Sat Jan 21, 2012 7:40 am
by tzupengwang
Thanks a lot~ I got AC
What a silly mistake I've made
10018 - Reverse and Add
Posted: Sun May 20, 2012 10:51 am
by FAR14
#include<stdio.h>
#include <conio.h>
int counter=1;
void palindrome(int a)
{
int flag=0;
int reverse=0;
int digit,temp;
int local=a;
int rev = 0;
while(a !=0)
{
digit=a%10;
reverse=reverse*10+digit;
a=a/10;
}
printf("reversed number=%d ",reverse);
int t=reverse+local;
printf("after addition=%d \n",t);
temp = t;
while( temp != 0 )
{
rev = rev * 10;
rev= rev+ temp%10;
temp = temp/10;
}
if ( t == rev )
{
printf("%d (iteration needed) %d(palindrome number).\n",counter, t);
}
else
{
counter++;
palindrome(t);
}
}
void main()
{
int input;
printf("Enter the number: ");
scanf("%d",&input);
palindrome(input);
getch();
}