在C ++中找到包含数字d的数字
考虑我们有一个数字d和上限n。我们必须找到所有0到n范围内包含d的数字。因此,如果n=20,数字为3,则数字将为[3,13]。
为了解决这个问题,我们将每个数字都当作字符串,然后如果字符串中存在数字,则将打印该数字,否则忽略该数字。
示例
#include<iostream>
using namespace std;
int getAllNumWithDigit(int n, int d) {
string str = "";
str += to_string(d);
char ch = str[0];
string p = "";
p += ch;
for (int i = 0; i <= n; i++) {
str = "";
str = str + to_string(i);
int index = str.find(p);
if (i == d || index!=-1)
cout << (i) << " ";
}
}
int main() {
int n = 100; int d = 3;
getAllNumWithDigit(n, d);
}输出结果
3 13 23 30 31 32 33 34 35 36 37 38 39 43 53 63 73 83 93
热门推荐
1 如何训练狗狗不叫
2 训练狗狗不叫
3 训练狗狗随行
4 怎么训练狗狗不叫
7 训练比熊狗狗教程
10 上海狗狗训练
11 怎么训练狗狗上厕所
12 训练狗狗趴下
13 狗狗服从训练
14 训练狗狗拒食
15 训练狗狗笑
16 训练狗狗大便
17 训练狗狗立
18 训练狗狗捡东西