1 条题解

  • 0
    @ 1982-1-2 21:09:33

    练39.1 书香阁座位

    解题思路

    座位构成15、17、19……。逐排加到总数312,记录当前行座位与排数,累加结束即得到末排37、共12排。

    正确性说明

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

    易错点

    按样例输出中文前缀,不只输出数字。

    复杂度

    固定总座位,时间O(1),空间O(1)。

    参考代码(C++11)

    #include <bits/stdc++.h>
    using namespace std;
    
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        int total = 0, seats = 15, rows = 0, last = 0;
        while (total < 312) {
            last = seats;
            total += seats;
            rows++;
            seats += 2;
        }
        cout << "最后一排的座位数:" << last << "\n排数:" << rows << "\n";
        return 0;
    }
    
    • 1

    信息

    ID
    268
    时间
    1000ms
    内存
    64MiB
    难度
    入门
    标签
    递交数
    0
    已通过
    0
    上传者