C. Hexadecimal's Numbers

时间:2019-08-16
本文章向大家介绍C. Hexadecimal's Numbers,主要包括C. Hexadecimal's Numbers使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

题目链接:http://codeforces.com/problemset/problem/9/C

题意:

输入n,输出1-n的自然数中各数位只包含0和1的数的个数。

思路:

不难知道1,10,11,100 这种是满足要求的。那么如何去求呢?

直接根据1,去找10,11

再根据10,去找100,101

再根据11,去找110,111

啊!好水的 dfs 为什么我当时就没有想到。。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <cstring>
 5 #include <math.h>
 6 #include <set>
 7 #include <vector>
 8 #include <stack>
 9 
10 #define LL long long
11 using namespace std;
12 const int maxn= 1e5 + 10;
13 
14 LL n;
15 
16 LL cnt = 0;
17 void dfs(LL x){
18     if (x>n)
19         return ;
20     cnt++;
21     dfs(x*10);
22     dfs(x*10+1);
23 }
24 
25 
26 int main(){
27     scanf("%lld",&n);
28     dfs(1);
29     printf("%lld\n",cnt);
30     return 0;
31 }

原文地址:https://www.cnblogs.com/-Ackerman/p/11366041.html