Code Forces 535 B. Tavas and SaDDas(数论)

Description
求出1~n内只包含4,7的数字有几个
Input
一个整数n
Output
输出1~n内只包含4,7的数字个数
Sample Input
77
Sample Output
6
Solution
枚举数字的位数,根据每位取值的可能性递推求解
Code

#include<stdio.h>
int main()
{ 
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        int num[10],i,res=0,ans=0;
        while(n)
        {
            num[res++]=n%10;
            n/=10;  
        } 
        int k=0,z=1;
        for(i=0;i<res;i++)
        {
            if(num[i]==4)
                k=1;
            else if(num[i]==7)
                k=2;
            ans+=k*z;
            z*=2;
        }
        printf("%d\n",ans);
    }
    return 0;
}

你可能感兴趣的:(Code Forces 535 B. Tavas and SaDDas(数论))