1 条题解

  • 0
    @ 2056-10-9 14:33:59

    练47.2 通力合作

    解题思路

    递归函数print(n)先打印1~n-1,再按宽5输出n,调用print(100)即可。n=0为终止条件。

    正确性说明

    计算规则直接来自题目定义。按所述顺序处理时,已完成部分的结果被准确保存在累加量、计数或当前状态中;下一步只加入新增贡献。最终得到完整要求的结果。

    易错点

    要求递归,不以for直接代替;每个数宽5。

    复杂度

    固定100次,时间O(1),递归空间O(1)。

    参考代码(C++11)

    #include <bits/stdc++.h>
    using namespace std;
    
    void print(int n) {
        if (n == 0)
            return;
        print(n - 1);
        cout << setw(5) << n;
    }
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        print(100);
        cout << "\n";
        return 0;
    }
    
    • 1

    信息

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