Codeforces Beta Round #9 (Div.2 Only) C.Hexadecimal's Numbers 二进制思想、技巧题

来源:互联网 发布:中档女装品牌 知乎 编辑:程序博客网 时间:2024/05/29 08:46
C. Hexadecimal's Numbers
time limit per test
1 second
memory limit per test
64 megabytes
input
standard input
output
standard output

One beautiful July morning a terrible thing happened in Mainframe: a mean virus Megabyte somehow got access to the memory of his not less mean sister Hexadecimal. He loaded there a huge amount of n different natural numbers from 1 to n to obtain total control over her energy.

But his plan failed. The reason for this was very simple: Hexadecimal didn't perceive any information, apart from numbers written in binary format. This means that if a number in a decimal representation contained characters apart from 0 and 1, it was not stored in the memory. Now Megabyte wants to know, how many numbers were loaded successfully.

Input

Input data contains the only number n (1 ≤ n ≤ 109).

Output

Output the only number — answer to the problem.

Sample test(s)
input
10
output
2
Note

For n = 10 the answer includes numbers 1 and 10.


题目大意:给出n,寻找小于n 的且所有数位均为0和1构成的数的个数,也就是说找出小于n 的长的跟二进制数一样的数的个数。

想了好久,因为脑子不好使,列举一下:

1~9有2^0个这样的数,

10~99有2^1个这样的数;

100~9999有2^2个这样的数

......

1后面n个0到9后面n个9一共有2^n-1个这样的数

求总数就把每个区间的相加即可。

处理n的时候通过数位判断是否大于1,如果大于1则置1,后面的数也都置1,;

否则,如果等于1置1,等于0再置0。

详细见代码

#include <iostream>#include <cstdio>#include <bitset>#include <cstring>#include <cmath>using namespace std;int main(){    ios::sync_with_stdio(false);    cin.tie(false);    char s[20];    bitset<20> b;    while(cin>>s)    {        int n = strlen(s);        bool fl = false;        for(int i = 0,j = n-1 ; i < n ; i++,j--)        {            if(s[i] > '1' || fl)            {                b[j] = 1;                fl = true;            }            else if(s[i] == '1')                b[j] = 1;            else                b[j] = 0;        }        int ans = 0;        for(int i = n-1 ; i >=0 ; i--)        {            if(b[i])            {                b.reset(i);                ans += (int)pow(2,i);            }        }        cout<<ans<<endl;    }    return 0;}



0 0
原创粉丝点击