UVa 10699 - Count the factors

Contents

  1. 1. Problem
  2. 2. Solution
  3. 3. Code

Problem

題目網址
中文網址

Solution

建完質數,因數分解即可。

Code

UVa 10699UVa 10699 - Count the factors
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include<cstdio>
#include<cmath>
#define N 1001

int main()
{
//建質數
bool sieve[N] = { true, true };
int prime[N], i, sqrt_n = sqrt(N);
for (i = 2; i <= sqrt_n; i++)
if (!sieve[i])
for (int j = i + i; j < N; j += i)
sieve[j] = true;

int count = 0;
for (i = 0; i < N; i++)
{
if (!sieve[i])
prime[count++] = i;
}

int n;
while (scanf("%d", &n) && n)
{
int ans = 0, temp = n;
//因數分解
for (i = 0; i < count&&n >= prime[i] * prime[i]; i++)
if (!(n%prime[i]))
{
ans++;
while (!(n%prime[i]))
n /= prime[i];
}
if (n > 1)
ans++;

printf("%d : %d\n", temp, ans);
}

return 0;
}