1 条题解

  • 0
    @ 2047-5-5 20:18:40

    练8.2 完成习题

    解题思路

    目标为总数的 30%,计算 x×3/10。

    正确性说明

    3/10 等于 30%,x 为 10 的倍数,结果为精确整数。

    易错点

    先乘后除,不能使用整数表达式 3/10 作为比例。

    复杂度

    本题规模固定,时间 O(1),额外空间 O(1)。

    参考代码(C++11)

    #include <bits/stdc++.h>
    using namespace std;
    
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        int x;
        cin >> x;
        cout << x * 3 / 10 << "\n";
        return 0;
    }
    
    • 1