top1编程
← 返回题目
题解

压缩技术

1 条题解

  • 0
    @ 2026-10-20 19:15:30

    1. 解题思路

    **核心方法:**从数字0开始,依次输出每一段指定数量的当前数字,段后翻转0与1。

    2. 坑点和注意事项

    • 第一段长度可以为0,也必须翻转状态;每输出n个字符换行,压缩段可跨行。位置最多40000,int足够。
    /*
    思路:从数字0开始,依次输出每一段指定数量的当前数字,段后翻转0与1。
    关键规则与坑点:第一段长度可以为0,也必须翻转状态;每输出n个字符换行,压缩段可跨行。位置最多40000,int足够。
    时间复杂度:O(n²+r),r为压缩段数。空间复杂度:O(1)。
    */
    #include <bits/stdc++.h>
    using namespace std;
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        int n, count, bit = 0, position = 0;
        // 读取题目给定数据。
        cin >> n;
        while (position < n * n && cin >> count)
        {
            for (int i = 0; i < count; i++)
            {
                cout << bit;
                position++;
                // 从数字0开始,依次输出每一段指定数量的当前数字,段后翻转0与1。
                if (position % n == 0)
                {
                    cout << '\n';
                }
            }
            bit ^= 1;
        }
    }
    

    3. 复杂度

    • **时间复杂度:**O(n²+r),r为压缩段数。
    • **空间复杂度:**O(1)。
    • 1