1 条题解

  • 0
    @ 2040-3-7 7:56:14

    练28.1 多边形内角和

    解题思路

    n 边形内角总和为 (n-2)×180,逐个减去 n-1 个已知角,余量就是未知角。

    正确性说明

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

    易错点

    输入仅有 n-1 个已知角,不能读 n 个。

    复杂度

    时间 O(n),额外空间 O(1)。

    参考代码(C++11)

    #include <bits/stdc++.h>
    using namespace std;
    
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        int n;
        cin >> n;
        int ans = (n - 2) * 180;
        for (int i = 0; i < n - 1; i++) {
            int a;
            cin >> a;
            ans -= a;
        }
        cout << ans << "\n";
        return 0;
    }
    
    • 1

    信息

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