|
@@ -1,79 +1,70 @@
|
|
#include <stdio.h>
|
|
#include <stdio.h>
|
|
-#include <stdlib.h>
|
|
|
|
|
|
+#include <stdbool.h>
|
|
|
|
|
|
-// 函数:判断一个数是否为素数
|
|
|
|
-int is_prime(int num) {
|
|
|
|
- int i;
|
|
|
|
- if (num <= 1) return 0;
|
|
|
|
- if (num == 2) return 1;
|
|
|
|
- if (num % 2 == 0) return 0;
|
|
|
|
- for (i = 3; i * i <= num; i += 2) {
|
|
|
|
- if (num % i == 0) return 0;
|
|
|
|
|
|
+// 鍒ゆ柇涓€涓�暟鏄�惁涓虹礌鏁�
|
|
|
|
+bool isPrime(int num) {
|
|
|
|
+ if (num <= 1) return false;
|
|
|
|
+ if (num <= 3) return true;
|
|
|
|
+ if (num % 2 == 0 || num % 3 == 0) return false;
|
|
|
|
+ int i;
|
|
|
|
+ for (i = 5; i * i <= num; i += 6) {
|
|
|
|
+ if (num % i == 0 || num % (i + 2) == 0)
|
|
|
|
+ return false;
|
|
}
|
|
}
|
|
- return 1;
|
|
|
|
|
|
+ return true;
|
|
}
|
|
}
|
|
|
|
|
|
-// 函数:获取一个数的所有素数因子
|
|
|
|
-void get_prime_factors(int num, int *factors, int *count) {
|
|
|
|
- int factor = 2;
|
|
|
|
- while (num > 1) {
|
|
|
|
- if (num % factor == 0) {
|
|
|
|
- factors[*count] = factor;
|
|
|
|
- (*count)++;
|
|
|
|
- num /= factor;
|
|
|
|
|
|
+// 鎵撳嵃绱犳暟鍒嗚В缁撴灉鐨勬渶灏忕礌鏁伴泦
|
|
|
|
+void printMinimumPrimeSet(int *compositeNumbers, int numCount) {
|
|
|
|
+ // 浣跨敤闆嗗悎鏉ュ瓨鍌ㄦ渶灏忕礌鏁伴泦锛屽埄鐢ㄩ泦鍚堢殑鑷�姩鍘婚噸鍜屾帓搴忕壒鎬�
|
|
|
|
+ bool primeSet[100000] = { false }; // 浣跨敤涓€涓�冻澶熷ぇ鐨勬暟缁勬潵瀛樺偍绱犳暟闆嗗悎
|
|
|
|
+ int maxNum = 0;
|
|
|
|
+
|
|
|
|
+ // 鎵惧嚭鎵€鏈夌殑绱犳暟鍥犲瓙
|
|
|
|
+ int i, k;
|
|
|
|
+ for (k = 0; k < numCount; ++k) {
|
|
|
|
+ int num = compositeNumbers[k];
|
|
|
|
+ if (isPrime(num)) {
|
|
|
|
+ primeSet[num] = true;
|
|
|
|
+ if (num > maxNum) maxNum = num;
|
|
} else {
|
|
} else {
|
|
- factor++;
|
|
|
|
- while (!is_prime(factor)) {
|
|
|
|
- factor++;
|
|
|
|
|
|
+ int factor = 2;
|
|
|
|
+ while (num > 1) {
|
|
|
|
+ if (isPrime(factor) && num % factor == 0) {
|
|
|
|
+ primeSet[factor] = true;
|
|
|
|
+ num /= factor;
|
|
|
|
+ factor = 2; // 閲嶆柊浠庢渶灏忕礌鏁板紑濮嬫壘鍥犲瓙
|
|
|
|
+ } else {
|
|
|
|
+ factor++;
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
+ if (factor > maxNum) maxNum = factor;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-}
|
|
|
|
-
|
|
|
|
-int main() {
|
|
|
|
- int i,j;
|
|
|
|
- int num;
|
|
|
|
-
|
|
|
|
- scanf("%d", &num);
|
|
|
|
|
|
|
|
- if (num <= 1) {
|
|
|
|
- printf("输入的数必须大于1且为合数。\n");
|
|
|
|
- return 1;
|
|
|
|
|
|
+ // 杈撳嚭鏈€灏忕礌鏁伴泦锛屾寜浠庡皬鍒板ぇ椤哄簭杈撳嚭
|
|
|
|
+ bool first = true;
|
|
|
|
+ for (i = 2; i <= maxNum; ++i) {
|
|
|
|
+ if (primeSet[i]) {
|
|
|
|
+ if (!first) printf(" ");
|
|
|
|
+ printf("%d", i);
|
|
|
|
+ first = false;
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
+ printf("\n");
|
|
|
|
+}
|
|
|
|
|
|
- int factors[32]; // 假设最大int的素数因子数量不会超过32个
|
|
|
|
- int count = 0;
|
|
|
|
-
|
|
|
|
- get_prime_factors(num, factors, &count);
|
|
|
|
-
|
|
|
|
- int occurrences[32] = {0}; // 用于记录每个素数因子的出现次数
|
|
|
|
- int unique_factors[32]; // 记录所有唯一的素数因子
|
|
|
|
- int unique_count = 0;
|
|
|
|
|
|
+int main() {
|
|
|
|
+ int numCount;
|
|
|
|
+ scanf("%d", &numCount);
|
|
|
|
|
|
- for (i = 0; i < count; i++) {
|
|
|
|
- int factor = factors[i];
|
|
|
|
- int found = 0;
|
|
|
|
- for (j = 0; j < unique_count; j++) {
|
|
|
|
- if (unique_factors[j] == factor) {
|
|
|
|
- occurrences[j]++;
|
|
|
|
- found = 1;
|
|
|
|
- break;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- if (!found) {
|
|
|
|
- unique_factors[unique_count] = factor;
|
|
|
|
- occurrences[unique_count] = 1;
|
|
|
|
- unique_count++;
|
|
|
|
- }
|
|
|
|
|
|
+ int compositeNumbers[20];
|
|
|
|
+ int i;
|
|
|
|
+ for (i = 0; i < numCount; ++i) {
|
|
|
|
+ scanf("%d", &compositeNumbers[i]);
|
|
}
|
|
}
|
|
|
|
|
|
- // 输出只出现一次的素数因子
|
|
|
|
- for (i = 0; i < unique_count; i++) {
|
|
|
|
- if (occurrences[i] == 1) {
|
|
|
|
- printf("%d ", unique_factors[i]);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- printf("\n");
|
|
|
|
|
|
+ printMinimumPrimeSet(compositeNumbers, numCount);
|
|
|
|
|
|
return 0;
|
|
return 0;
|
|
}
|
|
}
|