基础题,就用基本方法即可,计算约数时算到sqrt(n)即可,要注意(int)sqrt(n)和n的关系。
#include <cstdio> #include <cmath> #include <iostream> using namespace std; int main() { int T; cin >> T; while (T--){ int L, U; cin >> L >> U; int MAX = -1, num; for (int i = L; i <= U; ++i){ int cnt = 0; for (int j = 1; j <= sqrt(i); ++j){ if (i % j == 0){ ++cnt; if (i / j != j) ++cnt; } } if (cnt > MAX) MAX = cnt, num = i; } printf("Between %d and %d, %d has a maximum of %d divisors. ", L, U, num, MAX); } return 0; }