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

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

Re: 10018 can any1 plzz help me??

Post by brianfry713 »

I don't have conio.h on my system. Post the code you'd actually submit, not one with "Enter the number".
Check input and AC output for thousands of problems on uDebug!
pollob
New poster
Posts: 2
Joined: Wed Sep 12, 2012 9:55 am

UVA 10018

Post by pollob »

Here's my java code I don't why getting RunTime Error. Can any one help me ? Thanks in advance ...... :D


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;
}
}
}
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: UVA 10018

Post by brianfry713 »

Try input 4000000000, output should be 1 4000000004.
Check input and AC output for thousands of problems on uDebug!
mobarak.islam
New poster
Posts: 38
Joined: Wed Dec 05, 2012 11:29 pm

Re: 10018-Reverse and Add WA?

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

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

Re: 10018 - Reverse and Add

Post by brianfry713 »

post your full code including your main function. Use the code blocks.
Check input and AC output for thousands of problems on uDebug!
mobarak.islam
New poster
Posts: 38
Joined: Wed Dec 05, 2012 11:29 pm

Re: 10018 - Reverse and Add

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

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

Re: 10018 - Reverse and Add

Post by brianfry713 »

Your pendulam function returns 1 for num=1021.
Check input and AC output for thousands of problems on uDebug!
Zarif_iit
New poster
Posts: 1
Joined: Tue Dec 11, 2012 1:17 am

Re: 10018 - Reverse and Add

Post 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;
}
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10018 - Reverse and Add

Post by brianfry713 »

Change line 5 to:
char a[11],b[11];
Check input and AC output for thousands of problems on uDebug!
tamimcsedu19
New poster
Posts: 3
Joined: Fri Mar 01, 2013 6:17 pm

Re: 10018 - Reverse and Add

Post by tamimcsedu19 »

My code generates all the correct outputs,.. still getting WA. Can anyone figure out where am I wrong....

Code: Select all

Accepted
Got that.. thanks brian fry. I didnt notice my first char is a null. cmd in windows showed that as space :/
Last edited by tamimcsedu19 on Fri Mar 22, 2013 11:19 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

Post by brianfry713 »

Doesn't match the sample I/O. You're missing the space after the number of iterations.
Check input and AC output for thousands of problems on uDebug!
hansjuwiantho
New poster
Posts: 1
Joined: Fri Mar 29, 2013 7:44 am

Re: 10018 - Reverse and Add Compile Error

Post 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
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10018 - Reverse and Add

Post by brianfry713 »

You can click "My Submissions" and see the reason for your compile error. You can't use strrev on this compiler.
Check input and AC output for thousands of problems on uDebug!
CyberPunk
New poster
Posts: 7
Joined: Sat Mar 02, 2013 11:11 pm

Re: 10018 - Reverse and Add

Post 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! :(
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10018 - Reverse and Add

Post by brianfry713 »

Doesn't match the sample I/O.
Check input and AC output for thousands of problems on uDebug!
Post Reply

Return to “Volume 100 (10000-10099)”