PAT 2-07 Factor decomposition (C language Implementation)
PAT 2-07 Factor decomposition (C language implementation), the need for friends can refer to the next.
Title Description:
Given a positive integer n, the decomposition result of the element factor is given, that is, its factorization expression n = p1^k1* p2^k2*...*pm^km.
Input Format Description:
Enter a positive integer n in the range of long Int.
Output Format Description:
The element factorization expression of n is output in the given format, i.e. n = p1^k1* p2^k2*...*pm^km, where Pi is a factor and requires a small to large output, the exponent Ki is the number of pi, and when the ki==1 is only one of the factor pi, it does not output ki.
Sample input and output:
Serial number |
Input |
Output |
1 |
1024 |
1024=2^10 |
2 |
1323 |
1323=3^3*7^2 |
3 |
97532468 |
97532468=2^2*11*17*101*1291 |
4 |
1 |
1=1 |
5 |
3 |
3=3 |
Solution Description:
From small to large to look for the element factor, and the number of each element of the factor to do statistics.
Source:
#include <stdio.h> #include <math.h>int main (void) {int i,j; Long int m, N;int count;int isprime,flag;isprime = 1;flag = 0;scanf ("%ld", &n);p rintf ("%ld=", n); m = sqrt (n); for (i = 2; I <= m;i++) {if (n%i = = 0) {//printf ("%d", i); isprime = 0;count = 1;n = N/i;while (n%i = = 0) {n = n/i;count++;} if (flag) printf ("*"); Elseflag = 1;if (count = = 1) printf ("%d", I), elseprintf ("%d^%d", I,count);}} if (isprime) printf ("%d", n); return 0;}
PAT 2-07 Factor decomposition (C language Implementation)