Codeforces-535B Tavas and SaDDas

本文介绍了一种通过二分查找树的方法来确定给定幸运数在其所有同类数中位置的算法。幸运数是指十进制表示中仅包含4和7的正整数。通过输入一个幸运数n,算法能快速找出其在所有递增排列幸运数中的1基索引。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

The problem is:
You are given a lucky number n. Lucky 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.

Example
Input
4
Output
1

Input
7
Output
2

Input
77
Output
6

分析:乍一看好像很复杂,其实就是一棵二分查找树。每次判断最高位的数字:4—-cnt=2*cnt+1;7—-cnt=2*cnt+2。

Source:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

int main()
{
    char s[15];
    scanf("%s",s);
    int cnt=0;
    for(int i=0;i<strlen(s);i++)
    {
        if(s[i]=='4')
            cnt=2*cnt+1;
        else
            cnt=2*cnt+2;
    }
    printf("%d\n",cnt);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值