The requirement for this question is to write an integer greater than 1 into the form of several integer products. Because any number is multiplied by 1, the product of the integer remains unchanged, so the number after decomposition cannot be 1. The question requires all the arrangement forms, so 12 = 3 × 4 and 12 = 4 × 3 should be considered as two different decomposition types and should be output. For example, for integer n = 36, all of its factorization formulas are
36=3636=2*1836=18*236=3*1236=12*336=4*936=9*436=6*636=2*2*936=2*9*236=9*2*236=2*3*636=2*6*336=3*6*236=3*2*636=6*2*336=6*3*236=3*3*436=3*4*336=4*3*336=2*2*3*336=2*3*2*336=2*3*3*236=3*2*2*336=3*2*3*236=3*3*2*2
Algorithm principle:
This algorithm is implemented recursively. If each factor of integer N is placed in a stack and the number of elements is Len
1. output the form of multiplication of "n =" each factor.
2. send the last element of the array to the stack, send it to m, and split m into two integers M1, M2, and m1 and m2 into the stack. At this time, the number of elements in the stack is Len + 1, and the split function is called recursively.
For simplicity, we use arrays to simulate stacks. The code for function split is as follows:
Void split (int n, int arr [], int Len) {int I, last; printf ("% d =", n); for (I = 0; I <Len; I ++) {if (I> 0) printf ("*"); printf ("% d", arr [I]);} printf ("\ n"); For (last = arr [len-1], I = 2; I <= last/2; I ++) {If (last % I = 0) {arr [len-1] = I; // Add one of the last factors to the stack, replace the last element last arr [Len] = last/I; // split another last factor last/I into the stack (n, arr, Len + 1 ); // recursively call the split function }}}
Since there are many factors in the number of highly synthetic numbers, our test code uses the first 40. For more information about the concept of highly synthetic numbers, see high-Union numbers in Wikipedia.
Http://zh.wikipedia.org/wiki/%E9% AB %98%E5%90%88%E6%88%90%E6%95%B0
The complete code is as follows:
# Include <stdio. h> # include <stdlib. h> void split (int n, int arr [], int Len) {int I, last; printf ("% d =", n); for (I = 0; I <Len; I ++) {if (I> 0) printf ("*"); printf ("% d", arr [I]);} printf ("\ n"); For (last = arr [len-1], I = 2; I <= last/2; I ++) {If (last % I = 0) {arr [len-1] = I; // Add one of the last factors to the stack, replace the last element last arr [Len] = last/I; // split another last factor last/I into the stack (n, arr, Len + 1 ); // recursively call the split function }}int main () {int high_comp_num_arr [] = {2, 4, 6, 12, 24, 36, 48, 60,120,180,240,360,720,840,126 0, 1680,252 0, 5040,756 0, 10080,151 20, 20160,252 00, 27720,453 60, 50400,554 40, 83160,110 880, 166320,221 277200,332, 498960,554 665280,720, 2162160,}; int I, n, arr [32]; for (I = 0; I <sizeof (high_comp_num_arr)/sizeof (INT); I ++) {printf ("--------------------------------------- \ n "); N = high_comp_num_arr [I]; arr [0] = N; split (n, arr, 1);} return 0 ;}
If 12 = 3 × 4 and 12 = 4 × 3 are regarded as the same decomposition type, only one output is required. The split function should be modified as follows. To distinguish it from the original split, we name this function split2.
Void split2 (int n, int arr [], int Len) {int I, last, sqrt_last; printf ("% d =", n); for (I = 0; I <Len; I ++) {if (I> 0) printf ("*"); printf ("% d", arr [I]);} printf ("\ n"); last = arr [len-1]; sqrt_last = (INT) SQRT (last * 1.0); If (LEN <2) I = 2; else I = arr [len-2]; for (; I <= sqrt_last; I ++) {If (last % I = 0) {arr [len-1] = I; // import one of the last factor I into the stack and replace the last element last arr [Len] = last/I; // Add another last factor last/I to the stack split2 (n, arr, Len + 1); // recursively call the split function }}}