-
Notifications
You must be signed in to change notification settings - Fork 1
/
263_Ugly_Number_Easy.c
50 lines (45 loc) · 966 Bytes
/
263_Ugly_Number_Easy.c
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
42
43
44
45
46
47
48
49
50
/*
Write a program to check whether a given number is an ugly number.
Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly since it includes another prime factor 7.
Note that 1 is typically treated as an ugly number.
*/
//解法一 6ms
int fun(int num)
{
if(num%2==0)
num=num/2;
else if(num%3==0)
num=num/3;
else if(num%5==0)
num=num/5;
if(num%2==0||num%3==0||num%5==0)
num = fun(num);
if(num==1)
return 1;
return 0;
}
bool isUgly(int num) {
int i;
int k;
if(num<=0)
return false;
if(num==1)
return true;
k=fun(num);
if(k==1)
return true;
else
return false;
return;
}
//解法二 3ms
bool isUgly(int num) {
int i;
for(i=2;i<6&#i++)
while(num%i==0)
num/=i;
if(num==1)
return true;
else
return false;
}