Have a close look:
Code: Select all
Removed
You will get Ac.
Wish you good luck.
![:-?](./images/smilies/icon_confused.gif)
Moderator: Board moderators
Code: Select all
Removed
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.
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;
}
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;
}
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;
}
Code: Select all
Removed after AC