← 返回题目页

A0795 硬币翻转

标程

#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N;
    cin >> N;

    // 最少需要 N 次操作:每次恰好跳过一枚硬币,N 次里每枚恰好被跳过一次
    cout << N << '\n';

    vector<int> a(N, 0);   // 0 正面朝上,1 反面朝上
    for (int t = 0; t < N; t++) {
        // 第 t 次操作:跳过第 t 枚硬币,翻转其余 N-1 枚
        for (int i = 0; i < N; i++) {
            if (i != t) a[i] ^= 1;
        }
        for (int i = 0; i < N; i++) cout << a[i];
        cout << '\n';
    }
    return 0;
}

动画演示