这题核心问题在于你求约数个数是从1遍历到n,还是从1遍历到根号n。
//题目:https://www.nowcoder.com/questionTerminal/04c8a5ea209d41798d23b59f053fa4d6 #include <cstdio> int main(){ int amount; while(scanf("%d",&amount) != EOF){//读入数组个数N int i,j,temp; int num[amount]; for(i = 0; i < amount; i++){//读入要求约数的数 scanf("%d",&num[i]); temp = 0;//temp用来记录num[i]的约数个数,初始为0 for(j = 1;j*j <= num[i]; j++){//从1循环到num[i]^(1/2) if(j*j < num[i] && num[i]%j == 0) temp++; else if(j*j == num[i]){ temp = temp * 2 + 1; } } if(j > 1 && (j-1)*(j-1) != num[i]) temp = temp*2;//不可开方即为偶数个约数 printf("%d\n",temp);//逐行输出 } } return 0; }