知识点:

循环控制:循环控制

解法一:一位位数字查找

解法二:将数字转化为字符串,用字符串的.find()函数查找

#include <iostream>
#include <string>
using namespace std;

bool includeNine(int n);

int main() {
    int count = 0;

    for (int i = 1; i <= 2019; i++) {
        if (includeNine(i)) {
            count++;
        }
    }

    cout << count;

    return 0;
}

bool includeNine(int n) {
    bool res = false;
    string str;

    str = to_string(n);

    if (str.find('9') != -1) {
        res = true;
    } else {
        res = false;
    }

    return res;
}

// 解法一
/*
bool includeNine(int n);

int main() {
    int count = 0;

    for (int i = 1; i <= 2019; i++) {
        if (includeNine(i)) {
            count++;
        }
    }

    cout << count;

    return 0;
}

bool includeNine(int n) {
    bool res = false;
    int digit;

    while (n > 0) {
        digit = n % 10;
        n /= 10;
        if (digit == 9) {
            res = true;
            break;
        }
    }

    return res;
}
*/