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

Articuno
Learning poster
Posts: 78
Joined: Sun Nov 30, 2008 5:00 pm
Location: IUT-OIC, Dhaka, Bangladesh

Re: 10018 - Reverse and Add

Post by Articuno » Tue Dec 02, 2008 11:31 am

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:

Code: Select all

Removed
Set the flag to 0.
You will get Ac.
Wish you good luck. :-?
Last edited by Articuno on Wed Dec 03, 2008 8:03 am, edited 1 time in total.
May be tomorrow is a better day............ :)

abid_iut
Learning poster
Posts: 82
Joined: Wed Jul 16, 2008 7:34 am

Re: 10018 - Reverse and Add

Post by abid_iut » Tue Dec 02, 2008 6:31 pm

thankx Articuno
U r a great helper
pls continue helping me like this :D
i love to wait... wait for better... and better will come...
http://akanoi.webs.com/

lucasbls1
New poster
Posts: 1
Joined: Tue Mar 17, 2009 9:12 pm

Re: 10018 - Reverse and Add

Post by lucasbls1 » Tue Mar 17, 2009 9:17 pm

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);

}



}



}

}

sefakilic
New poster
Posts: 7
Joined: Wed Mar 11, 2009 8:12 pm

Re:

Post by sefakilic » Wed Mar 18, 2009 1:48 am

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.

@mjad
New poster
Posts: 44
Joined: Thu Jul 22, 2010 9:42 am

10018 TE why?

Post by @mjad » Thu Jul 29, 2010 9:05 am

hi i am trying to solve it but it is TE.
help me, about it please..........

User avatar
sohel
Guru
Posts: 856
Joined: Thu Jan 30, 2003 5:50 am
Location: New York

Re: 10018 TE why?

Post by sohel » Thu Jul 29, 2010 8:47 pm

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!

@mjad
New poster
Posts: 44
Joined: Thu Jul 22, 2010 9:42 am

Re: 10018 TE why?

Post by @mjad » Sat Jul 31, 2010 7:43 am

thanks to reply
finally i got AC

Martuza_iu
New poster
Posts: 4
Joined: Tue Sep 21, 2010 4:17 pm
Location: Islamic University, Kushtia
Contact:

Re: 10018 TE why?

Post by Martuza_iu » Fri Sep 24, 2010 8:21 pm

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;
}

tanvirfromhell
New poster
Posts: 5
Joined: Sat Apr 16, 2011 7:42 pm

Re: 10018 - Reverse and Add

Post by tanvirfromhell » Sun May 15, 2011 10:29 am

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;
}

aansu
New poster
Posts: 3
Joined: Wed Oct 13, 2010 1:42 pm

Re: 10018 - Reverse and Add

Post by aansu » Wed May 25, 2011 4:21 am

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;
}

Munna_csedu
New poster
Posts: 1
Joined: Fri May 27, 2011 10:31 am

Re: 10018 - Reverse and Add

Post by Munna_csedu » Fri May 27, 2011 11:42 am

#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

tzupengwang
New poster
Posts: 36
Joined: Fri Dec 02, 2011 1:30 pm
Location: Kaohsiung, Taiwan

10018-Reverse and Add WA?

Post by tzupengwang » Sat Dec 17, 2011 8:08 am

Can anyone help?
Thanks~

Code: Select all

Removed after AC
Last edited by tzupengwang on Sat Jan 21, 2012 7:41 am, edited 1 time in total.

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10018-Reverse and Add WA?

Post by brianfry713 » Tue Jan 10, 2012 2:53 am

a is a long long int and should be printed with format code %lld
Check input and AC output for thousands of problems on uDebug!

tzupengwang
New poster
Posts: 36
Joined: Fri Dec 02, 2011 1:30 pm
Location: Kaohsiung, Taiwan

Re: 10018-Reverse and Add WA?

Post by tzupengwang » Sat Jan 21, 2012 7:40 am

Thanks a lot~ I got AC
What a silly mistake I've made

FAR14
New poster
Posts: 1
Joined: Sun May 20, 2012 10:37 am

10018 - Reverse and Add

Post by FAR14 » Sun May 20, 2012 10:51 am

#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();
}

Post Reply

Return to “Volume 100 (10000-10099)”