在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 带狗狗训练
4 听力训练狗狗
6 怎训练狗狗
7 腹肌训练狗狗
8 网球训练狗狗
9 记叙文训练狗狗
10 狗狗太凶怎么训练
11 核心力量训练狗狗
12 狗狗乱叫怎么训练
13 发音训练狗狗
14 怎么训练狗狗叼东西给主人
15 荆州训练狗狗
16 篮球训练狗狗
17 做训练狗狗
18 训练狗狗坐姿