一个小于根号n的约数必对应一个大于根号n的约数,因此时间复杂度可降为根号n
#include <iostream>
#include "cmath"
using namespace std;
int main() {
int n;
while (cin >> n) { // 注意 while 处理多个 case
// cout << a + b << endl;
for(int i=0;i<n;i++){
int a;
cin>>a;
int num=0;
int bound=sqrt(a);
for(int j=1;j<=bound;j++)
{
if(a%j==0) num+=2;
if(j==sqrt(a)) num-=1;
}
// if(a==1) num=1;
cout<<num<<endl;
}
}
}
// 64 位输出请用 printf("%lld")

京公网安备 11010502036488号