01数
如果一个正整数,其各个数位上的数字均满足要么是 0,要么是 1,则称该数字为 01 数。
例如,1 和 10 都是 01 数。
给定一个整数 n。
请你计算,1∼n 中有多少个 01 数。
输入格式
一行,一个整数 n。
输出格式
一个整数,表示 01 数的数量。
数据范围
前六个测试点满足 1≤n≤100。
所有测试点满足 1≤n≤109。
输入样例:
10
输出样例:
2
题解
数据范围从 1到1e9 一共10位数
用枚举法枚举1~1024内的所有数,因为1024等于2^10,所以把枚举过程中的每个数的二进制看做10进制
求得数的大小,并判断是否在1到n之间即可
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
/*
*/
int main()
{
int n;
cin>>n;
int res = 0;
for(int i=1; i<(1<<10); i++){
int x = 0;
for(int j=0; j<10; ++j){
x = x*10 +(i>>j & 1);
}
if(x<=n){
res++;
}
}
cout<<res<<endl;
return 0;
}