Codeforces Beta Round #9 (Div.2 Only) C.Hexadecimal's Numbers 二进制思想、技巧题
来源:互联网 发布:中档女装品牌 知乎 编辑:程序博客网 时间:2024/05/29 08:46
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 data contains the only number n (1 ≤ n ≤ 109).
Output the only number — answer to the problem.
10
2
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;}
- Codeforces Beta Round #9 (Div.2 Only) C.Hexadecimal's Numbers 二进制思想、技巧题
- Codeforces Beta Round #9 (Div. 2 Only): C
- Codeforces Beta Round #9 (Div. 2 Only)
- Round #9 C.Hexadecimal's Numbers
- Codeforces Beta Round #16 (Div. 2 Only)——C
- Codeforces Beta Round #18 (Div. 2 Only)——C
- Codeforces Beta Round #74 (Div. 2 Only)——C
- Codeforces Beta Round #87 (Div. 2 Only) C. Party
- Codeforces Beta Round #4 (Div. 2 Only) C. Registration system
- C.Stripe(Codeforces Beta Round #18 (Div. 2 Only))
- Codeforces Beta Round #91 (Div. 2 Only) C. Lucky Sum
- Codeforces Beta Round #22 (Div. 2 Only)-C. System Administrator
- codeforces 9C. Hexadecimal's Numbers
- CodeForces 9C - Hexadecimal's Numbers
- Codeforces 9C Hexadecimal's Numbers
- Codeforces-9C-Hexadecimal's Numbers
- CodeForces 9C-Hexadecimal's Numbers-构造
- CodeForces 9C Hexadecimal's Numbers
- poj 3904 Sky Code【容斥原理】
- 15、OC 包装类(NSNumber、NSValue)
- 关于C++的sizeof()操作符
- 1030存代码
- poj-3669
- Codeforces Beta Round #9 (Div.2 Only) C.Hexadecimal's Numbers 二进制思想、技巧题
- 数据库
- 数据挖掘作业——FP Tree算法之C++实现
- 拥抱Android:编译nodejs搭建移动平台
- LeetCode Find Minimum in Rotated Sorted Array II
- DOM学习笔记三
- 最小生成树
- shell大小中括号作用
- Minimum Window Substring leetcode