题意:此题是判断给定的数字是第几个幸运数,首先判断位数,然后判断出现4和出现7的情况,进行统计。
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define LL long long
LL N(LL k)
{
LL sum=1;
for(LL i=1;i<=k;i++)
{
sum*=2;
}
return sum;
}
LL P(LL k)//计算数字的数位有多少位
{
LL temp=k;
LL sum=0;
while(temp)
{
sum++;
temp/=10;
}
return sum;
}
LL X(LL k)//取除了后面的数
{
LL temp=k;
LL sum=0;
while(temp)
{
sum++;
temp/=10;
}
LL s=1;
for(LL i=1;i<sum;i++)
{
s*=10;
}
return s;
}
int O(int n)//取首位
{
int u=0;
while(n)
{
u=n%10;
n/=10;
}
return u;
}
int main()
{
LL n;
while(scanf("%lld",&n)!=EOF)
{
LL sum=0;
for(LL i=1;i<=P(n)-1;i++)
{
sum+=N(i);
}
LL temp=n;
LL right=N(P(n)),left=1;
while(temp)
{
LL z=O(temp);
LL m=(right+left)>>1;
//printf("%lld\n",m);
if(z==4)
{
right=m-1;
}
else if(z==7)
{
left=m+1;
}
temp=temp-z*X(temp);
}
printf("%lld\n",sum+left);
}
return 0;
}