在C ++中查找从1到n的整数数,其中包含0和1的数字
假设我们有一个数字n。我们的任务是找到1到n之间的整数数,该整数数仅包含数字0s和1s。因此,如果n=15,则输出将为。因为数字是1,10,11
为了解决这个问题,我们将使用递归函数使用0和1创建整数。以下代码将帮助我们更好地理解这一点。
示例
#include<iostream>
using namespace std;
int numberOfValues(int p, int n) {
if (p > n)
return 0;
return 1 + numberOfValues(p * 10, n) + numberOfValues(p * 10 + 1, n);
}
int main() {
int n = 120;
cout << "Number of values using 0s and 1s: " << numberOfValues(1, n);
}输出结果
Number of values using 0s and 1s: 7
热门推荐
10 朋友节最简短祝福语
11 秋分祝福语简短文案
12 哥哥祝福语简短霸气句子
13 女侠老师祝福语简短
14 猪年幽默祝福语简短
15 高考毕业祝福语简短励志
16 升学祝福语简短的个性
17 小红书生日祝福语简短
18 撩妹生日祝福语简短