Codeforces 535B Tavas and SaDDas 水题一枚

题目链接:Tavas and SaDDas

Once again Tavas started eating coffee mix without water! Keione told him that it smells awful, but he didn't stop doing that. That's why Keione told his smart friend, SaDDas to punish him! SaDDas took Tavas' headphones and told him: "If you solve the following problem, I'll return it to you."

The problem is:

You are given a lucky number nLucky numbers are the positive integers whose decimal representations contain only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.

If we sort all lucky numbers in increasing order, what's the 1-based index of n?

Tavas is not as smart as SaDDas, so he asked you to do him a favor and solve this problem so he can have his headphones back.

Input

The first and only line of input contains a lucky number n (1 ≤ n ≤ 10^9).

Output

Print the index of n among all lucky numbers.

题意描述:求出小于等于n并每一位上只包含4或7的数的个数。

算法分析:比赛的时候脑子大半夜短路了,刚开始一直在想枚举n的每个位上的数和4、7的关系,搞来搞去没出结果,后来才发现这样的数其实不多,每位上要求是4或7,一个数最多9位,也就2的9次方这么多呗,暴力求解然后判断这样的数和n的大小关系就OK了。

 1 #include<iostream>

 2 #include<cstdio>

 3 #include<cstring>

 4 #include<cstdlib>

 5 #include<cmath>

 6 #include<algorithm>

 7 #define inf 0x7fffffff

 8 using namespace std;

 9 const int maxn=555;

10 

11 int n,m;

12 int an[10][maxn];

13 

14 int main()

15 {

16     while (scanf("%d",&n)!=EOF)

17     {

18         if (n<4) {printf("0\n");continue; }

19         if (n>=4 && n<7) {printf("1\n");continue; }

20         int cnt=2;

21         memset(an,0,sizeof(an));

22         an[1][1]=4 ;an[1][2]=7 ;

23         for (int i=2 ;i<10 ;i++)

24         {

25             int c=1;

26             int t=1,j=i-1;

27             while (j) {t*=10;j--; }

28             int flag=0;

29             for (int j=1 ;j<=pow(2,i-1) ;j++)

30             {

31                 an[i][c]=4*t+an[i-1][j];

32                 if (an[i][c]>n) {flag=1;break; }

33                 cnt++;c++;

34             }

35             for (int j=1 ;j<=pow(2,i-1) ;j++)

36             {

37                 an[i][c]=7*t+an[i-1][j];

38                 if (an[i][c]>n) {flag=1;break; }

39                 cnt++;c++;

40             }

41             if (flag) break;

42         }

43         printf("%d\n",cnt);

44     }

45     return 0;

46 }

 

你可能感兴趣的:(codeforces)