Data_Structure/Vorlesungen/DS/Beispiele/prime-factoring-array.c
2025-01-20 21:25:33 +08:00

42 lines
714 B
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
Program: prime-factoring-array.c (Report comments/bugs to chikh@yuntech.edu.tw)
Function:
*/
#include <stdio.h>
int indx = 0;
int factor[100];
void divide(int n, int divisor)
{
if (divisor > n)
return;
else if (n%divisor != 0)
divide(n,divisor+1);
else {
printf("%d ",factor[indx++] = divisor);
divide(n/divisor,divisor);
}
}
int main()
{
int i, j, n;
printf("輸入欲因式分解的正整數 ==> ");
scanf("%d",&n);
printf("\n質因數為:");
divide(n,2);
printf("\n\n%d = ",n);
for (i = 0; i < indx;) {
printf("%d",factor[i]);
for (j = 1; i+j < indx; j++)
if (factor[i+j] != factor[i]) break;
if (j > 1) printf("^%d",j);
if ((i=i+j) < indx) printf("×");
}
return 0;
}