1 条题解

  • 0
    @ 2009-9-8 18:49:19

    【例33.1】 卡(qia)常数

    解题思路

    枚举全部四位整数 1000~9999,拆分为前两位 n/100 与后两位 n%100,检查二者和的平方是否等于 n。

    正确性说明

    按上述推导执行:每次仅处理当前输入或当前步骤,并把它对答案的贡献计入对应状态。所有需要处理的情况均被覆盖,结束时保存的量就是题目所求。

    易错点

    必须限制四位数;后两位含零仍按整数处理。

    复杂度

    固定9000候选,时间 O(1),空间 O(1)。

    参考代码(C++11)

    #include <bits/stdc++.h>
    using namespace std;
    
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        bool first = true;
        for (int n = 1000; n <= 9999; n++) {
            int s = n / 100 + n % 100;
            if (s * s == n) {
                if (!first)
                    cout << " ";
                cout << n;
                first = false;
            }
        }
        cout << "\n";
        return 0;
    }
    
    • 1

    信息

    ID
    225
    时间
    1000ms
    内存
    64MiB
    难度
    进阶
    标签
    递交数
    0
    已通过
    0
    上传者