#include<iostream>
#include<math.h>
using namespace std;
int main(){
int n,loc,sum;
while(cin >> n){
sum = 0;
loc = 2;
while(n != 1){
if(loc > sqrt(n) + 1){
sum++;
break;
}
if(n % loc == 0){
sum++;
n /= loc;
}
else
loc++;
}
cout << sum << endl;
}
return 0;
}
例4.9 整除问题
#include<iostream>
#include<math.h>
using namespace std;
int main(){
int n,a,loc,x,i,flag,loc1,ii,min;
while(cin >> n >> a){
int p[10];
int pnum[10] = {0};
int pnum1[10] = {0};
loc = -1;
x = 2;
flag = 0;
while(a != 1){
if(a % x == 0){
if(flag == 0){
loc++;
flag = 1;
}
p[loc] = x;
pnum[loc]++;
a = a / x;
}
else{
x++;flag = 0;
}
}
for(i=2;i<=n;i++){
loc1 = 0;
ii = i;
while(loc1 <= loc){
if(ii % p[loc1] == 0){
pnum1[loc1]++;
ii /= p[loc1];
}
else
loc1++;
}
}
min = 10000;
for(i=0;i<=loc;i++)
if(pnum1[i] / pnum[i] < min)
min = pnum1[i] / pnum[i];
cout << min << endl;
}
return 0;
}
约数的个数
#include<iostream>
#include<math.h>
using namespace std;
int num(int x){
int i;
int num = 0;
for(i=1;i<=sqrt(x);i++)
if(x % i == 0)
num += 2;
int M = int(sqrt(x));
if(M * M == x)
num--;
return num;
}
int main(){
int n,i;
int x[1005];
while(cin >> n){
for(i=0;i<n;i++)
cin >> x[i];
for(i=0;i<n;i++)
cout << num(x[i]) << endl;
}
return 0;
}