每个输入的n,
对sqrt(n)以下的进行判断,若存在一个约数,则大于sqrt(n)的也有一个约数,每次+2即可
对sqrt(n)的边界值单独判断,若sqrt(n)*sqrt(n)刚好等于n本身,则需要减去一个计数(sqrt(n)算了两次)
#include <iostream> #include <cstdio> #include<math.h> using namespace std; int main() { int n; while (cin >> n) { int num; for (int i = 0; i < n; i++) { cin >> num; int sum = 0; int bound = sqrt(num); for (int j = 1; j <= bound; j++) { if (num % j == 0) { sum += 2; } } if (bound * bound == num)sum--; cout << sum << endl; } } return 0; }