Codeforces-535B Tavas and SaDDas

来源:互联网 发布:北京办公软件速成班 编辑:程序博客网 时间:2024/06/10 07:34

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;}
0 0
原创粉丝点击