在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
热门推荐
10 新年门口花束祝福语简短
11 盘锦结婚祝福语大全简短
12 父母生日祝福语 简短独特
13 家庭恩爱祝福语简短英文
14 高考俄语祝福语大全简短
15 虎年祝福语 诗句唯美简短
16 生日婚礼祝福语简短精辟
17 虎年喝酒拜年祝福语简短
18 教师闺蜜祝福语简短