Codeforces Round #299 (Div. 2) B. Tavas and SaDDas【DFS/*进制思维/位运算/一个数为幸运数,当且仅当它的每一位要么是4,要么是7 ,求小于等于n的幸运数个数】

B. Tavas and SaDDas
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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 ≤ 109).

Output

Print the index of n among all lucky numbers.

Examples
input
4
output
1
input
7
output
2
input
77
output
6

 

【分析】:直接DFS爆枚所有在[1,109]内的幸运数,最多只有29个,非常少,然后判断每个幸运数是否小于等于n,统计答案即可。

【代码】:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int ans,n;
void dfs(ll now)
{
    if(now>1e9) return ;
    if(now<=n) ans++;
    dfs(now*10+4);
    dfs(now*10+7);
}
int main()
{
    ios::sync_with_stdio(false);
    cin>>n;
    dfs(4);
    dfs(7);
    cout<<ans<<endl;
}
View Code

 

posted @ 2017-12-02 19:13  Roni_i  阅读(195)  评论(0编辑  收藏  举报