0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Atcoder解いてみた ABC 088 C - Takahashi's Information

Posted at

ABC 088 C - Takahashi's Information 解いてみた

問題はこちら

回答

元リンクはこちら

int C[3][3];
int A[3], B[3];
//---------------------------------------------------------------------------------------------------
void _main() {
    rep(i, 0, 3) rep(j, 0, 3) cin >> C[i][j];
 
    rep(a1, 0, 101) rep(a2, 0, 101) rep(a3, 0, 101) {
        int b1 = C[0][0] - a1;
        int b2 = C[0][1] - a1;
        int b3 = C[0][2] - a1;
 
        A[0] = a1;
        A[1] = a2;
        A[2] = a3;
        B[0] = b1;
        B[1] = b2;
        B[2] = b3;
 
        int ok = 1;
        rep(i, 0, 3) rep(j, 0, 3) if (A[i] + B[j] != C[i][j]) ok = 0;
        if (ok) {
            printf("Yes\n");
            return;
        }
    }
 
    printf("No\n");
}

別解(上記とほぼ同じだが、、、)

#include <iostream>
using namespace std;

int main() {

    // 簡単に考えよう

    int C[3][3];
    int B[3][3]; //今回、Cの各要素に対応するbを求め、j方向で見ていった時にbが一定かどうか判定しているため、このような形でBの配列を確保している

    for (int j = 0; j < 3; j++) {
        for (int i = 0; i < 3; i++) {
            cin >> C[j][i];
        }
    }

    // aが決まれば、c-aでbも決まる
    // aとbの取りうる値すべてのループは効率悪い
    
    // aの添え字:i方向 (a1,a2, a3はj方向に一定であることが前提)
    for (int a1 = 0; a1 <= 100; a1++) {
        for (int a2 = 0; a2 <= 100; a2++) {
            for (int a3 = 0; a3 <= 100; a3++) {

                for (int j = 0; j < 3; j++) {
                    B[j][0] = C[j][0] - a1;
                    B[j][1] = C[j][1] - a2;
                    B[j][2] = C[j][2] - a3;
                }

                bool can = true;
                for (int j = 0; j < 3; j++) {
                    if (B[j][0] != B[j][1] || B[j][1] != B[j][2] || B[j][2] != B[j][0]) {
                        can = false;
                    }
                }
                if (can == true)
                {
                    cout << "Yes" << endl;
                    return 0;
                }

            }
        }
    }

    cout << "No" << endl;
    return 0;
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?