編寫一個讀入整數k的程序,並打印出正好具有k個除數的1到100000(含)之間的正整數數。獲得結果1個,除數計數程序 - C
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(void){
int k=0, N=0, i=0, r=0, n_divisors=0, result=0;
printf("Enter the number of divisors: ");
/*scanning in an integer value*/
scanf(" %d",&k);
/*loop to count the no. of divisors in an integer from 1 to 100000*/
for(N=1; N<=100000; N++){
n_divisors=0;
/*loop for incrementing the no. divisors in an integer by 2 */
for(i=1; i<sqrt(N); i++){
/*Testing if i is a divisor of the integer.*/
r=(N%i);
if(r==0)
n_divisors+=2;
}
/* If the no. of divisors is equal to k, then increment result*/
if(n_divisors==k)
result++;
/* else reset the value of no. of divisors to 0*/
}
/* printing the value for no. of integers form 1 to 100000 with K no. of divisors*/
printf("There are %d numbers between 1 and 100000 inclusive which have exactly \n",result );
printf("%d divisors.",k);
return 0;
}
很奇怪,用C註釋('>註釋/ /')。我的編譯器不喜歡它。 – MarianD