I wrote a program in C for recycled numbers(the problem of previous
codejam contest) .it outputs a value of 96 for A=100, B=500 whereas the
correct answer is 156. Please help me where I am wrong.
#include<stdio.h>
int power(int a,int b)
{
if(b==0) return 1;
if(b==1) return a;
return a*power(a,b-1);
}
int recycled_pairs(int x,int y)
{
int n=x,d=0,z,pairs=0,nn;
while(n) {d++;n=n/10;}
n=x;
do
{
if(n%10)
{
n=(n%10)*power(10,d-1)+n/10;
}
else
{
z=0;nn=n;
while(!(nn%10)) {z++;nn=nn/10;}
nn=n; nn=nn/(power(10,z)); nn=nn%10; //nn is here last non-zero
digit
n = nn*power(10,d-1)+n/power(10,z+1);
}
if(n!=x && n>x && n<=y) pairs++;
}while(n!=x);
return pairs;
}
main()
{
int tc,i,j,A,B,n,m,cnt,nd,x,lead;
scanf("%d",&tc);
for(i=0;i<tc;i++)
{
scanf("%d %d",&A,&B);
cnt=0;nd=0;x=A;
while(x){nd++;x=x/10;}
for(j=A;j<B;j++)
{ lead=(j/power(10,nd-1));
if(lead) {
cnt=cnt+recycled_pairs(j,B);
}
}
printf("case #%d : %d\n",i+1,cnt);
}
}
--
You received this message because you are subscribed to the Google Groups
"Google Code Jam" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msg/google-code/-/rP9qKsuK3IcJ.
For more options, visit https://groups.google.com/groups/opt_out.