约数即因子,质数,约数类题目先想到用所给数开根号来缩小循环次数,简化计算,对于本题,以sqrt(num)为界,若比它小有一个因子,则比它大一定存在另一个因子,此时结果加2,最后再判断sqrt(num)是否为其一个因子若是则结果加1,即可。
程序代码
#include <iostream>
using namespace std;
int main()
{
int n;
while (cin >> n) {
for (int i = 0; i < n; ++i) {
int temp;
int count = 0;
cin >> temp;
int j;
for ( j = 1; j*j < temp; ++j) { //以sqrt(temp)为界
if (temp % j == 0) count += 2;
}
if (j * j == temp) count++;
cout << count << endl;
}
}
}