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
  • 相关阅读:
    setAnimationTransition:forView:cache: 运行动画时背景色问题
    架构师速成4.6-软技能和硬技能
    Java获取某年某周的第一天
    openssl之BIO系列之12---文件描写叙述符(fd)类型BIO
    centos 使用 CP 命令 不提示 覆盖
    [3 Jun 2015 ~ 9 Jun 2015] Deep Learning in arxiv
    P1314 聪明的质监员
    P2858 [USACO06FEB]奶牛零食Treats for the Cows
    1163 访问艺术馆
    P1352 没有上司的舞会
  • 原文地址:https://www.cnblogs.com/Roni-i/p/7955230.html
Copyright © 2011-2022 走看看