题目链接

贝壳找房

题解

简单题。直接模拟即可。

代码

#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
int n, m;
int xuqiu[10005];
bool fc(int a, int b) {
    return a < b;
}
int main() {
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> m;
        for (int j = 1; j <= m; j++) {
            cin >> xuqiu[j];
        }
        sort(&xuqiu[1], &xuqiu[m + 1], fc);
        int a = 1;
        int num = 1;
        for (;a<xuqiu[1];) {
            a = a + num++ + 2;
        }
        if (a == xuqiu[1]) {
            bool boo = true;
            for (int j=1; j<=m;j++) {
                if (a != xuqiu[j]) {
                    boo = false;
                    break;
                }
                a = a + num++ + 2;
            }
            if (boo)cout << "yes" << endl;
            else cout << "no" << endl;
        }
        else cout << "no" << endl;
    }
}

标签: implementation

添加新评论