Cordingle analog test beautiful numbers

ucdosucdos 注册会员
2023-02-27 13:36


using namespace std;

int main() {
    int n, m;
    cin >> n >> m;

    unordered_set<char> perfect_digits;
    for (int i = 0; i < n; i++) {
        char c;
        cin >> c;

    for (int i = 0; i < m; i++) {
        string s;
        cin >> s;
        bool is_perfect = true;
        for (char c : s) {
            if (perfect_digits.find(c) == perfect_digits.end()) {
                is_perfect = false;
        if (is_perfect) {
            cout << s << endl;
        } else {
            cout << "none" << endl;

    return 0;

first reads n and m, along with n flawless one-digit numbers, stored using an unordered_set.

then loops in m multidigits, and for each multidigit, iterates over each of its digits. If any digit is not in the set of flawless digits, that multidigit is not a beautiful number, marking is_perfect as false. If all the digits of the entire multidigit number are in the set of flawless digits, the multidigit number is a beautiful number, and the multidigit number is printed.

Time complexity is O(m * len), where len is the maximum length of multiple digits.

ding1ei 注册会员
2023-02-27 13:36

the reference answer GPT ᴼ ᴾ ᴱ ᴺ ᴬ ᴵ < br / > a possible Python implementation is as follows:

def is_beautiful_number(number, beautiful_digits):
    # 判断数字 number 是否是优美的数字
    for digit in str(number):
        if digit not in beautiful_digits:
            return False
    return True

with open('number.in', 'r') as f:
    # 读入数据
    n, m = map(int, f.line().split())
    beautiful_digits = set(f.readline().split())
    numbers = list(map(int, f.readline().split()))

    # 遍历多位数,找出优美的数字
    beautiful_numbers = []
    for number in numbers:
        if is_beautiful_number(number, beautiful_digits):

    # 输出结果
    if beautiful_numbers:
        for number in beautiful_numbers:

first reads the data, storing the beautiful numbers in a collection beautiful_digits, and the multidigits in a list numbers.

Next, we define a function is_beautiful_number(number, beautiful_digits) to determine whether a number number is a beautiful number. Iterate over each digit of number, returning False if any digit is not in the beautiful_digits collection. Otherwise, return True.

Then, we go through each number in the list of numbers to determine if it is a beautiful number. If it is, add it to the beautiful_numbers list.

Finally, if the beautiful_numbers list is not empty, output each number in the order it was entered. Otherwise, the string 'none' is printed.

About the Author

Question Info

Publish Time
2023-02-27 13:36
Update Time
2023-02-27 13:36