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中,有几个是可以用二进制的表示:例如:10,从1,2,3,4,5,6,7,8,9,10,只有1,10可以被二进制识别出来,所以输出结果为2.
解题思路:
经过大量数据的运算,我发现能被二进制识别的是:1,10,11,100,101,110,111.........所以规律是:为1*10和1*10+1的条件下,满足的时候才能识别,所以思路就是:用函数调用,一共需要调用2次函数,所以AC代码:
#include <iostream>
using namespace std;
int n,m;
int count,flag;
int binary(int m)
{
if(m>n)
return 0;
else
count++;
binary(m*10);
binary(m*10+1);
}
int main()
{
while(cin>>n)
{
count=0;
binary(1);
cout<<count<<endl;
}
return 0;
}