GESP5级2026年3月份编程题超详解
·
一个数 能使
化为有限不循环小数,当且仅当
的质因数只包含 2 和 5。以下是用 C++ 实现计算区间
内终止数数量的代码:
#include <iostream>
using namespace std;
int main() {
int l, r;
cin >> l >> r;
int res = 0;
for (int i = l; i <= r; ++i) {
int x = i;
while (x % 2 == 0) x /= 2;
while (x % 5 == 0) x /= 5;
res += x == 1;
}
cout << res << endl;
return 0;
}
分析
- 时间复杂度:
,其中
是区间内整数的数量。
- 空间复杂度:
,只使用了常数级的额外空间。
更多推荐

所有评论(0)