1 条题解
-
0
练60.3 余数个数
解题思路
每个输入对42取余,余数在0~41,用布尔表标记,最后统计true数量。
正确性说明
按顺序完成所述运算后,每个输入元素都被处理在相应位置或统计项中。保存的中间状态与已处理数据对应,最终输出与题目定义一致。
易错点
不同原数可以有相同余数,只算一次。
复杂度
固定10数42余数,时间O(1),空间O(1)。
参考代码(C++11)
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); bool seen[42] = {}; for (int i = 0; i < 10; i++) { long long x; cin >> x; seen[x % 42] = true; } int ans = 0; for (bool x : seen) ans += x; cout << ans << "\n"; return 0; }
- 1