1 条题解

  • 0
    @ 2103-3-28 4:05:02

    【例11.2】数字对调

    解题思路

    提取百位 n/100、十位 n/10%10、个位 n%10,再按个位、十位、百位重组数值。

    正确性说明

    各位数字交换后按新的十进制位权相加,得到交换百位与个位的结果。

    易错点

    这是数值输出,原个位为 0 时新数不保留前导零。

    复杂度

    本题输入规模固定,时间 O(1),额外空间 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;
        cout << n % 10 * 100 + n / 10 % 10 * 10 + n / 100 << "\n";
        return 0;
    }
    
    • 1