AtCoder入门赛157C

· · 题解

题目

见C - Guess The Number

算法

关于这题只需注意几点:

  1. 这个十进制整数不能有前导零
  2. 同一位数可以赋相同的值,如果赋不同的值则输出-1
  3. 至于其他方面只需要处理好细节就行了

C++ 代码

#include <iostream>
#include <vector>

using namespace std;

int n, m;
int s[10], c[10];

bool check(int x) {
    vector<int> xs;

    if (x == 0) xs.push_back(0);
    else {
        while (x) {
            xs.push_back(x % 10);
            x /= 10;
        }
    }

    if (xs.size() != n) return false;

    for (int i = 0; i < m; i++) {
        if (xs[n - s[i]] != c[i]) return false;
    }

    return true;
}

int main() {
    cin >> n >> m;
    for (int i = 0; i < m; i++) cin >> s[i] >> c[i];

    for (int i = 0; i < 1000; i++) {
        if (check(i)) {
            cout << i << endl;
            return 0;
        }
    }

    cout << -1 << endl;

    return 0;
}