Page 10 of 12
Re: 10018 can any1 plzz help me??
Posted: Mon May 21, 2012 11:02 pm
by brianfry713
I don't have conio.h on my system. Post the code you'd actually submit, not one with "Enter the number".
UVA 10018
Posted: Wed Sep 12, 2012 10:01 am
by pollob
Here's my java code I don't why getting RunTime Error. Can any one help me ? Thanks in advance ......
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static boolean Palindrome (BigInteger num){
String str =num.toString();
int lengthNum =str.length()-1;
int length =0;
boolean palindrime =true;
while (length<lengthNum){
if (str.charAt(length++) !=str.charAt(lengthNum--)){
palindrime =false;
break;
}
}
return palindrime;
}
public static void main (String[] args){
Integer num, reverse = 0, t, cnt =0;
BigInteger result =BigInteger.ZERO;
boolean palindrime =false;
Scanner scan =new Scanner (System.in);
t =scan.nextInt();
while ((t--)>0){
num =scan.nextInt();
while (!palindrime){
String str =num.toString();
StringBuffer buffer =new StringBuffer (str);
buffer =buffer.reverse();
reverse =Integer.parseInt(buffer.toString());
cnt++;
result =BigInteger.valueOf(num+reverse);
palindrime =Palindrome (result);
num =result.intValue();
}
System.out.println (cnt+" "+result);
cnt =0;
palindrime =false;
}
}
}
Re: UVA 10018
Posted: Wed Sep 12, 2012 8:40 pm
by brianfry713
Try input 4000000000, output should be 1 4000000004.
Re: 10018-Reverse and Add WA?
Posted: Wed Dec 05, 2012 11:46 pm
by mobarak.islam
Here I'm getting WA. Can any one help me ?please please please.
#include<stdio.h>
#include<stdlib.h>
int Reverse()
{
long long int s,bp,data,c,s1[10],loop=0,n,i,j;
long long int pendulam(long long int num);
scanf("%lld",&n);
for( j=0;j<n;j++)
{
scanf("%lld",&s);
label:
loop++;
bp=s;
c=0;
data=0;
while(s!=0)
{
s1[c]=s%10;
s=s/10;
c++;
}
for( i=0;i<c;i++)
{
data=data*10+s1;
}
data+=bp;
if(pendulam(data)==0)
{
s=data;
goto label;
}
else
{
printf("%lld %lld\n",loop,data);
loop=0;
}
}
return 0;
}
long long int pendulam(long long int num)
{
long long int s2[11],count=0,ncount,j;
while(num!=0)
{
s2[count]=num%10;
num=num/10;
count++;
}
ncount=count-1;
j=0;
while(j!=ncount-(1-count%2))
{
if(s2[j]==s2[ncount])
{
j++;
ncount--;
}
else
return 0;
}
return 1;
}
Re: 10018 - Reverse and Add
Posted: Sat Dec 08, 2012 5:43 pm
by brianfry713
post your full code including your main function. Use the code blocks.
Re: 10018 - Reverse and Add
Posted: Wed Dec 12, 2012 9:00 pm
by mobarak.islam
please help me. i'm getting wa.
#include<stdio.h>
#include<stdlib.h>
int main()
{
long long int s,bp,data,c,s1[10],loop=0,n,i,j;
long long int pendulam(long long int num);
scanf("%lld",&n);
for( j=0;j<n;j++)
{
scanf("%lld",&s);
label:
loop++;
bp=s;
c=0;
data=0;
while(s!=0)
{
s1[c]=s%10;
s=s/10;
c++;
}
for( i=0;i<c;i++)
{
data=data*10+s1;
}
data+=bp;
if(pendulam(data)==0)
{
s=data;
goto label;
}
else
{
printf("%lld %lld\n",loop,data);
loop=0;
}
}
return 0;
}
long long int pendulam(long long int num)
{
long long int s2[11],count=0,ncount,j;
while(num!=0)
{
s2[count]=num%10;
num=num/10;
count++;
}
ncount=count-1;
j=0;
while(j!=ncount-(1-count%2))
{
if(s2[j]==s2[ncount])
{
j++;
ncount--;
}
else
return 0;
}
return 1;
}
Re: 10018 - Reverse and Add
Posted: Thu Dec 13, 2012 12:51 am
by brianfry713
Your pendulam function returns 1 for num=1021.
Re: 10018 - Reverse and Add
Posted: Thu Dec 13, 2012 1:46 pm
by Zarif_iit
I get Runtime Error for this code. Everything seems to work fine when I run it from Code Blocks.
Help me out please.
#include<stdio.h>
#include<string.h>
#include <stdlib.h>
char a[10],b[10];
int palindrome()
{
int i,j;
int l=strlen(a);
if(l%2==0)
for(i=0, j=l-1;i<=l/2;i++, j--)
{
if(a
!=a[j]) return 0;
}
else
for(i=0, j=l-1;i<l/2;i++, j--)
{
if(a!=a[j]) return 0;
}
return 1;
}
int main()
{
int i,j,l,count;
int test;
long long int temp;
//freopen("test.txt", "r", stdin);
scanf("%d", &test);
while(test--)
{
count=0;
scanf("%s", &a);
while(1)
{
l=strlen(a);
for(i=l-1, j=0;i>=0;i--, j++)
{
b[j]=a;
}
b[l]='\0';
temp=atoll(a)+atoll(b);
sprintf(a, "%lld", temp);
count++;
if(palindrome()==1)
{
printf("%d %s\n", count, a);
break;
}
}
}
return 0;
}
Re: 10018 - Reverse and Add
Posted: Fri Dec 14, 2012 12:34 am
by brianfry713
Change line 5 to:
char a[11],b[11];
Re: 10018 - Reverse and Add
Posted: Thu Mar 21, 2013 6:48 pm
by tamimcsedu19
My code generates all the correct outputs,.. still getting WA. Can anyone figure out where am I wrong....
Got that.. thanks brian fry. I didnt notice my first char is a null. cmd in windows showed that as space :/
Re: 10018 - Reverse and Add
Posted: Thu Mar 21, 2013 11:59 pm
by brianfry713
Doesn't match the sample I/O. You're missing the space after the number of iterations.
Re: 10018 - Reverse and Add Compile Error
Posted: Fri Mar 29, 2013 7:49 am
by hansjuwiantho
#include<stdio.h>
#include<string.h>
int main()
{
char x[500];
char temp[500];
int count=0,i,j,jj;
scanf("%d\n",&j);
for(jj=0;jj<j;jj++)
{
gets(x);
strcpy(temp,x);
strrev(temp);
count=0;
while(strcmp(temp,x)!=0)
{
char hasil[100];
int counter[100]={0};
hasil[0]='0';
for(i=strlen(temp)-1;i>=0;i--)
{
hasil[i+1]=x
-48+temp-48;
if(hasil[i+1]>=10)
{
hasil[i+1]%=10;
counter++;
}
hasil[i+1]+=48;
}
for(i=strlen(temp)-1;i>=0;i--)
{
hasil+=counter;
}
hasil[strlen(temp)+1]='\0';
if(counter[0]==0)
{
for(i=0;i<=strlen(temp);i++)
{
hasil=hasil[i+1];
}
}
strcpy(x,hasil);
strcpy(temp,x);
strrev(temp);
count++;
}
printf("%d %s\n",count,x);
}
return 0;
}
all test case i use got correct answer 
please help me
Re: 10018 - Reverse and Add
Posted: Fri Mar 29, 2013 10:16 pm
by brianfry713
You can click "My Submissions" and see the reason for your compile error. You can't use strrev on this compiler.
Re: 10018 - Reverse and Add
Posted: Mon Apr 15, 2013 12:54 am
by CyberPunk
Why WA? my code seems to give correct output for all possible input.
Code: Select all
#include <stdio.h>
#include <string.h>
int pal_check(char *s)
{
int len=strlen(s),i;
for(i=0;i<len/2;i++) if(s[i]!=s[len-i-1]) return 0;
return 1;
}
char *rev_add(char *s1, int len)
{
char x[1000],s2[1000];
int i,j,tmp,carry=0;
for(i=0;i<len;i++) s2[len-i-1]=s1[i];
s2[i]=NULL;
for(i=len-1,j=0;i>=0;i--,j++) {
tmp=s1[i]+s2[i]-96+carry;
x[j]=tmp%10+48;
carry=tmp/10;
}
if(carry) {
x[j]=carry+48;
j++;
}
x[j]=NULL;
len=strlen(x);
for(i=0;i<len/2;i++) {
tmp=x[i];
x[i]=x[len-i-1];
x[len-i-1]=tmp;
}
return x;
}
int main()
{
char string[1000],T,count,len;
scanf("%d",&T);
gets(string);
while(T--) {
gets(string);
count=0;
do {
len=strlen(string);
strcpy(string,rev_add(string,len));
count++;
} while(!pal_check(string));
printf("%d %s\n",count,string);
}
return 0;
}
please help!

Re: 10018 - Reverse and Add
Posted: Wed Apr 17, 2013 11:02 pm
by brianfry713
Doesn't match the sample I/O.