/** title: Decomposition of a positive integer factorization. For example: Enter 90 and print out 90=2*3*3*5. Program Analysis: The decomposition of n factorization, should first find a minimum prime number k, and then the following steps to complete: (1) If the prime number is exactly equal to N, then the decomposition of the factorization process has ended, printing can be. (2) if n <> K, but n can be divisible by K, then the value of K should be printed, and n divided by the quotient of K, as a new positive integer you n, repeat the first step. (3) If n cannot be divisible by K, the first step is repeated with k+1 as the value of K. * Date: July 27, 2015 14:48:08* file: lianxi04.cpp* cutter_point*/#include <iostream> #include <fstream> #include <sstream>using namespace std;//Here we can use recursive int fenjie (int sum, ofstream &out) {if (sum = = 1) return 0;else{for (int i = 2; I < sum + 1; ++i) {if ((sum% i) = = 0)//is not factorization, see can be taken as 0{//if it is exactly divisible by I, here will not appear divisible by 2, can be divisible by 4, but the situation of output 4, because this will have been output out of the loop before the sum = Sum/i;o UT << "*" << i;cout << "*" << I;break;}} Return Fenjie (sum, Out);}} int main () {Ifstream input ("Lianxi04in.txt"); Ofstream out ("Lianxi04out.txt"); int k;string num;//reads the corresponding data from the file input out << "========= start prime number decomposition ==============" << endl;cout << "======== start prime number decomposition ==============" << Endl; while (getline (input, num)) {istringstream istr (num);//reads one line of characters while (ISTR >> k)//Read all the numbers of all the number {out << K << "in a row are: 1"; cout << k << "All prime numbers are: 1"; Fenjie (k, out); Out << Endl & lt;< "==============================" << endl;cout << endl << "==============================" << Endl;}} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"Written test" 4, positive integer decomposition factorization