【十二题解】 | #字符串分隔#
质数因子
http://www.nowcoder.com/practice/196534628ca6490ebce2e336b47b3607
其实只要循环之后每次判断一下是不是质数就可以过关了,这个测试用例大多是分散的比较开的式子
#include <stdio.h>
#include<math.h>
int isprime(int num);
int main() {
int number;
scanf("%d", &number);
if(isprime(number) == 1){
printf("%d", number);
return 0;
}
for (int i=2; i<=number/2; i++) {
while (number%i == 0) {
printf("%d ", i);
number /= i;
if(isprime(number) == 1){
printf("%d", number);
return 0;
}
}
}
return 0;
} int isprime(int num){
if (num <= 3) {
return num > 1;
}
if (num % 6 != 1 && num % 6 != 5) {
return 0;
}
int sqr = (int)sqrt(num);
for (int i = 5; i <= sqr; i += 6) {
if (num % i == 0 || num % (i + 2) == 0) {
return 0;
}
}
return 1;
}