1010: The Almost Lucky Numbers
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 16 Solved: 11
[ Submit][ Status][ Web Board]
Description
John and Brus believe that the digits 4 and 7 are lucky and all others are not. According to them, an almost lucky number is a number that contains at most one non-lucky digit in its decimal representation. Return the total number of almost lucky numbers between a and b, inclusive.
Input
Each line contains two integers a,b<=1,000,000
Output
For each line of input,output the total number of almost lucky numbers between a and b, inclusive.
Sample Input
4 7 8 19
Sample Output
4 4
思路:数据不大,直接搜即可。或者另一种方法,排列组合,不成熟,当搜索的数和当前数位数相同的时候没想出来怎么算
#include<stdio.h>
int l(int x)
{
int cnt(0);
while(x>0) {x/=10;cnt++;}
return cnt;
}
bool is(int x)
{
bool y(true);
while(x>0)
{
if(!(x%10==4||x%10==7))
{if(y) y=!y;else return false;}
x/=10;
}
return true;
}
int main()
{
int x,y,t,i,a,lx,ly;
int e[9],u[9];
e[0]=1;u[0]=1;e[1]=1;
for(i=1;i<9;i++)
{
u[i]=u[i-1]*2;
}
for(i=2;i<9;i++)
{
e[i]=e[i-1]*10;
}
while(scanf("%d%d",&x,&y)==2)
{
a=0;
if(x>y) {t=x;x=y;y=t;}
lx=l(x);ly=l(y);
if(ly-lx>=2)
{
for(i=lx+1;i<=ly-1;i++)
{
a+=u[i]+8*u[i-1]*i-u[i-1];
}
for(i=x;i<e[lx+1];i++)
{
if(is(i)) a++;
}
for(i=e[ly];i<=y;i++)
{
if(is(i)) a++;
}
}
else
{
for(i=x;i<=y;i++)
{
if(is(i)) a++;
}
}
printf("%d\n",a);
}
return 0;
}