Programming question-Grouping Problem, output formula.

Source: Internet
Author: User

Programming questions:
Enter two integers N and P. (n <= P) in the command line. The program outputs a series of formulas that meet the following conditions:
Formula: A1 * B1 + A2 * B2 +... + an * bn
Requirements:
The formula must meet the following requirements:
A1 * B1 + A2 * B2 +... + an * bn = N
A1 + A2 +... + an = P
Where the number of columns: A1 A2... An is an integer number of> = 1.
B1 B2... BN is an integer Series> = 1, and satisfies: BN> Bn-1
Multiple formulas are in a parallel relationship and have no output sequence requirements.
This program is named grouping ). A batch of balls of multiple colors (unlimited quantity) need to be packed in N grids. each package contains only p-colored balls. the duplicates in these combinations.
For example, A1 appears B1 times + A2 appears B2 times.

For example:
Input
Grouping 7 5
4*1 + 1*3
3*1 + 2*2

Solution: (users who do not like the idea can directly look at the code)
Design a function to input n p ks. The function can be called recursively.
N is the remaining N value p is the remaining P value K is the smallest B value S is the input string.

When 7 or 5 is input, the function is called as follows:

F (7, 5, 1, null)
Function attempt:
5*1 P-5 = 0, 5*1 <7 F (7, 5, 2, null) Continue the next attempt.
4*1 P-4 = 1 call f (7-4*1, 5-4, 2, "4*1"), recursively return and continue the next attempt.
3*1 call f (4,2, 2, "3*1 ")
2*1 call f (5, 3, 2, "2*1 ")
1*1 call f (6, 4, 2, "1*1 ")
Return.

Call: F (7-4*1, 5-4, 2, "4*1") Resolution
F (3, 1, 2, "4*1 ")
Try
1*2 P-1 = 0, F (3, 1, 3, "4*1") ---> output 4*1 + 1*3

Call F (4,2, 2, "3*1 ")
Try
2*2 = 4 ---> output 3*1 + 2*2

 1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4  5 void grouping(int N,int P,int K ,char* prestr) { 6 #define grouping_buf_size 128 7     // trying: N N-1 N-2 .... 8     char buf[grouping_buf_size]; 9     for(int i=P;i>0;i--) {10         if(prestr==NULL) {11             sprintf(buf,"%d*%d",i,K); 12         } 13         else {14             sprintf(buf,"%s + %d*%d",prestr,i,K); 15         }16         if(P-i==0) {17             if(N-i*K == 0) {18                 printf("%s\n",buf); 19             }20             else if(N-i*K > 0) {21                 grouping(N,P,K+1,prestr);22             }23             else if(N-i*K < 0) {24             }25         }26         else if(N-i*K>0){27 28             grouping(N-i*K,P-i,K+1,buf);29         }30 31     }32 33 #undef grouping_buf_size 34 }35 36 int main(int argc,char** argv) {37 int n=atoi(argv[1]);38 int p=atoi(argv[2]);39 grouping(n,p,1,NULL);40 41 return 1;42 }
Grouping code

Test:

$ Grouping 7 7
7*1
$ Grouping 7 6
5*1 + 1*2
$ Grouping 7 5
4*1 + 1*3
3*1 + 2*2
$ Grouping 7 4
3*1 + 1*4
2*1 + 1*2 + 1*3
1*1 + 3*2

 

Programming question-Grouping Problem, output formula.

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.