First, the problem description
A positive integer greater than 1 n can be decomposed into N=X1*X2*...*XM. Given an integer greater than 1, then count the number of its constituent forms, such as input 12:
12=12;
12=6*2;
12=4*3;
12=3*4; 12=3*2*2;
12=2*6; 12=2*3*2;
12=2*2*3;
A total of 8 different decomposition types.
As input 10:
10=10;
10=5*2;
10=2*5;
A total of 3 different decomposition types.
Second, the problem analysis
From the decomposition of 12 can be seen from 12 to 2 to find 12 factor, if divisible, then the quotient as a new value, continue to look for its factors, until it can no longer be decomposed, that is, a prime number, so this program uses recursive thinking.
Third, program design
#include <iostream>
using namespace Std;
int count=1; //counting starting from 1, now contains the number itself
void out (int n); //recursive function, function declaration
void Main ()
{
int number;
cout<< "Please input the number:";
cin>>number;
Out (number); //Execute function
cout<< "Total number of decomposition cases:" <<count;
while (1);
}
Recursive functions
void out (int n)
{
for (int j=n-1;j>1;j--) //descending from n-1 to 2, looking for approximate
if (n%j==0)
{
count++; //If it is possible to be removed, add 1
Out (n/j); ///recursive function to continue the recursion of the quotient
}
}
Iv. Results of the procedure
Recursive integer factor decomposition