zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    C++之private虚函数
    图的遍历DFS与BFS(邻接表)
    拓扑排序(邻接矩阵)
    paypal Encryption 支付编程全解
    JAVA 正则表达式 分组
    正则表达式 <A HREF>
    Linux 逻辑卷管理 实例
    LINUX命令笔记(2)
    哀悼日:快速设置黑白页面
    XEN笔记
  • 原文地址:https://www.cnblogs.com/Roni-i/p/7955230.html
Copyright © 2011-2022 走看看