1 条题解

  • 0
    @ 2001-4-25 22:47:00

    练33.3 水仙花数

    解题思路

    枚举100~999,拆出百十个位 a,b,c,检查 a³+b³+c³ 是否等于原数,符合按升序输出。

    正确性说明

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

    易错点

    只考虑三位数,不能把0~9也输出。

    复杂度

    固定900候选,时间 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 = 100; n <= 999; n++) {
            int a = n / 100, b = n / 10 % 10, c = n % 10;
            if (a * a * a + b * b * b + c * c * c == n) {
                if (!first)
                    cout << " ";
                cout << n;
                first = false;
            }
        }
        cout << "\n";
        return 0;
    }
    
    • 1

    信息

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