题目链接
题目大意
输入n,计算出n之内只有0和1组成的数字的数量
分析
k从1开始,只要小于n,就给sum++,并给k加上1,而k用go函数模拟二进制的进位
代码
#include<iostream>
#include<cstdio>#include<cstring>#include<string>#include<algorithm>#include<cctype>#include<cmath>#include<cstdlib>#include<queue>#include<ctime>#include<vector>#include<set>#include<map>#include<stack>using namespace std;int k;void go(){ int i,j; i=1,j=1; int ok=1; k+=1; while(ok){ if(k/j%10<2)ok=0; else { k=1*i*10+k-2*i; i*=10; j*=10; } }}int main(){ int n,m,i,j,sum=0; cin>>n; k=1; while(k<=n){ sum++; go(); } cout<<sum<<endl; return 0;}