1 条题解
-
0
【例33.2】 两位数
解题思路
从最小两位数 10 起枚举到 99,检查十位加个位为 a、两位差绝对值为 b,首个符合就输出。
正确性说明
按上述推导执行:每次仅处理当前输入或当前步骤,并把它对答案的贡献计入对应状态。所有需要处理的情况均被覆盖,结束时保存的量就是题目所求。
易错点
大减小用 abs;不能让十位为 0。
复杂度
固定90候选,时间 O(1),空间 O(1)。
参考代码(C++11)
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int a, b; cin >> a >> b; for (int n = 10; n <= 99; n++) { int x = n / 10, y = n % 10; if (x + y == a && abs(x - y) == b) { cout << n << "\n"; break; } } return 0; }
- 1