974 - Kaprekar Numbers

All about problems in Volume 9. 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: 974 - Kaprekar Numbers

Post by brianfry713 » Wed Feb 20, 2013 9:03 pm

Try using BufferedWriter.

Before you read any input, create a bool array of size 40001 that stores whether or not that is a Kaprekar Number. That will speed up your main loop.
Check input and AC output for thousands of problems on uDebug!

saju10
New poster
Posts: 8
Joined: Mon Nov 18, 2013 12:09 am

Re: 974 - Kaprekar Numbers

Post by saju10 » Thu Feb 06, 2014 9:54 am

why wrong answer? here given my code.
#include<stdio.h>
int main()
{
int test,p=1,m,n,i,a[21],start,end,t;;
a[1]=1;
a[2]=9;
a[3]=45;
a[4]=55;
a[5]=99;
a[6]=297;
a[7]=703;
a[8]=999;
a[9]=2223;
a[10]=2728;
a[11]=4879;
a[12]=4950;
a[13]=5050;
a[14]=5292;
a[15]=7272;
a[16]=7777;
a[17]=9999;
a[18]=17344;
a[19]=22222;
a[20]=38962;
scanf("%d",&test);
while(test--)
{
scanf("%d%d",&m,&n);
printf("case #%d\n",p++);
start=0;
end=0;
if(m<=38962)
if(n>38962)
n=38962;
t=0;
for(i=1; i<20; i++)
if(m>=a&&m<a[i+1])
{
if(m==a)
start=i;
else
start=i+1;
break;
}
for(i=20; i>1; i--)
if(n>=a[i-1]&&n<=a)
{
if(n==a)
end=i;
else
end=i-1;
break;
}
if(start!=0)
for(i=start; i<=end; i++)
{
t=1;
printf("%d\n",a);
}
if(t==0)
printf("no kaprekar numbers\n");
if(test)
printf("\n");
}
return 0;
}

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

Re: 974 - Kaprekar Numbers

Post by brianfry713 » Thu Feb 06, 2014 11:07 pm

Try input:

Code: Select all

1
38962 38962
Check input and AC output for thousands of problems on uDebug!

Post Reply

Return to “Volume 9 (900-999)”