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
  • 相关阅读:
    使用mustache js模板引擎
    Application Cache API (二)
    scrollMonitor 滚动事件
    NPM中的那些库
    lodash 函数功能 boilerjs
    SeaJS 里版本号和时间戳管理的最佳实践
    开源前端框架纵横谈
    URI.js – 全能的URL操作库
    执行用户定义例程或聚合 "" 期间出现 .NET Framework 错误:
    一般ALV错误有两种情况
  • 原文地址:https://www.cnblogs.com/Roni-i/p/7955230.html
Copyright © 2011-2022 走看看