[CodeForces-143A]题解(C++)

发布时间 2023-05-06 08:28:59作者: -沉默-

Part I Preface

原题目(Luogu)
原题目(CodeForces)

Part II Sketch

  • 设有一个 \(2 \times 2\) 的棋盘,上面可以填入 \(1-9\) 的数字。
  • 给出 \(6\) 个数字,为每行每列以及每个对角线上的数字之和,求相应的摆放方式,无解输出 \(-1\)

Part III Analysis

观察此题数据规模,不难发现数据很小,可以直接采用 \(O(9^4)\) 暴力枚举即可。

Part IV Code

#include <bits/stdc++.h>
using namespace std;
int r1, r2;
int c1, c2;
int d1, d2;
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> r1 >> r2 >> c1 >> c2 >> d1 >> d2;
    for(int i = 1; i <= 9; i++){
        for(int j = 1; j <= 9; j++){
            for(int k = 1; k <= 9; k++){
                for(int l = 1; l <= 9; l++){
                    if((i + j == r1) && (k + l == r2) && (i + k == c1) && (j + l == c2) && (i + l == d1) && (j + k == d2) && (i != j && i != k && i != l) && (j != k && j != l) && (k != l)){
                        cout<<i<<' '<<j<<'\n'<<k<<' '<<l;
                        return 0;
                    }
                }
            }
        }
    }
    cout<<-1;
    return 0;
}

Part V Record


Record