题目:分解质因数
#include <stdio.h>
#include <stdbool.h> void printPrimeFactors(int n) {bool isFirstFactor = true; while (n % 2 == 0) {if (!isFirstFactor) {printf(" * "); }printf("%d", 2);isFirstFactor = false; n = n / 2;}for (int i = 3; i * i <= n; i = i + 2) {while (n % i == 0) {if (!isFirstFactor) {printf(" * "); }printf("%d", i);isFirstFactor = false; n = n / i;}}if (n > 2) {if (!isFirstFactor) {printf(" * "); }printf("%d", n);}
}int main() {int num;printf("请输入一个不小于2的整数: ");scanf_s("%d", &num);if (num < 2) {printf("输入的数必须不小于2。\n");return 1;}printf("%d的质因数分解为: %d = ", num, num);printPrimeFactors(num); printf("\n");return 0;
}