The C program realizes the prime number and the decomposition problem of integer _c language

Source: Internet
Author: User

This paper describes the C program to achieve the number of integers and decomposition problems, shared for everyone to reference. The specific methods are as follows:

Requirements: For a given integer, the output of all such prime numbers and decomposition, for the homogeneous decomposition of the output only once (for example, 5 has only one decomposition 2+3, and 3+2 is the 2+3 homogeneous decomposition).

For example:

For integer 8, you can use the following three types of decomposition:
(1) 8 = 2 + 2 + 2 + 2
(2) 8 = 2 + 3 + 3
(3) 8 = 3 + 5

When I saw this, my first reaction was to solve the knapsack problem.

Ideas are as follows:

F (N, array) = f (n-array[i], array), save the result, array is to save the value of the element, that is, all primes, refer to the previous question, if the prime number can only be used once, then set up a corresponding bool array can be marked as true every time you use, Then the recursive function needs to be reset to false, which is not necessary for the subject, but the array of saved results needs to be dropped from the prime number of the current attempt.

The code is as follows:

/* Copyright (c) alexingcool.  
All Rights Reserved. * #include <iostream> #include <iterator> #include <algorithm> #include <vector> using Namespac

e std;
vector<int> result;

Vector<int> Prvec;

 void Outputresult (int N, vector<int> &prime, vector<int> &result) {if (n < 0) return;
 if (N = = 0) {copy (Result.begin (), Result.end (), ostream_iterator<int> (cout, ""));
 cout << Endl;
 Return

 for (int i = 0; i < prime.size (); i++) {//To improve efficiency, you can make a judgment condition here as soon as possible to return if (N-prime[i] < 0) break;
 Result.push_back (Prime[i]);
 Outputresult (N-prime[i], Prime, result);
 Result.pop_back ();
 } void OutputResult2 (int n, vector<int> &prime, vector<int> &result, int position) {if (n < 0)

 Return
 if (N = = 0) {copy (Result.begin (), Result.end (), ostream_iterator<int> (cout, ""));
 cout << Endl;
 Return for (int i = position I < prime.size (); i++) {//To improve efficiency, you canThis makes a judgment condition and returns if (N-prime[i] < 0) break as soon as possible;
 Result.push_back (Prime[i]);
 OUTPUTRESULT2 (N-prime[i], prime, result, i);
 Result.pop_back ();
 BOOL IsPrime (int number) {if (number <= 1) return false;
 if (number = = 2) return true;
 for (int i = 2; i < number, i++) {if (number% i = = 0) return false;
return true; } void Generateprime (int number, vector<int> &result) {for (int i = 2; i < number-1; i++) {if IsPrime (i
 )) Result.push_back (i);
 } void Main () {int # = 8;
 Generateprime (number, Prvec);
 Outputresult (number, Prvec, result);
 cout << "Remove isomorphism" << Endl;
OUTPUTRESULT2 (number, Prvec, result, 0);

 }

The results of the operation are shown in the following illustration:

Note: For the isomorphism problem, I am looking at the output of the results, the Outputresult function, the result of 332, the result is not correct, an obvious feature is that after 3, the number of the following can not be less than 3, then only need to save 3 of the current position can be, Then, in the current position loop, the isomorphism problem can be eliminated.

It is believed that this article has certain reference value to everybody C program algorithm design learning.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.