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
  • 相关阅读:
    解决Xcode升级7.0后,部分.a静态库在iOS9.0的模拟器上,link失败的问题
    2014年工作总结
    工作还是事业
    海豚社区阶段性开发总结
    Xcode开发和调试总结
    iOS证书深究
    何为分类,UIImageView举例
    UIWebView的探索
    SugarSync网盘之NSDateFormatter
    ASP.NET程序从IIS6移植到IIS7时出现500.22错误(转)
  • 原文地址:https://www.cnblogs.com/Roni-i/p/7955230.html
Copyright © 2011-2022 走看看