[N subset of m element sets], subset of m element sets

Source: Internet
Author: User

[N subset of m element sets], subset of m element sets

/* N subset of m elements: Assume that a set has m elements and n elements are retrieved from the set, what are the possible subsets of the n elements? Solution: Assume that there are five element set points and the possible subsets of the three elements are as follows: {1 2 3}, {1 2 4}, {1 2 5}, {1 3 4}, {1 3 5}, {1 4 5}, {2 3 4 4 }, {2 3 5}, {2 4 5}, and {3 4 5} subsets are ordered in alphabetical order, in this way, we can observe some rules: If the rightmost element is smaller than m, we will continue to add 1 as the code table. If the right one has reached the maximum value, after the position where the value of 1 is shifted to the left, the elements on the right must be adjusted to the descending order. Therefore, the key point is that the position must be incremented by 1, add 1 to the rightmost position? Or other locations? When writing a program, you can use a variable positon to record the position where the value of position is added. The initial value of position is set to n-1 because the array is used, the rightmost index value is the largest n-1. If the value at the position is smaller than m, 1 is continuously added. If the value is greater than m, the position value is reduced by 1, that is, move the position to the left. Because the right element is adjusted after the Left shift, we must check whether the rightmost element is smaller than m. If yes, the position is adjusted back to n-1, if not, positon remains unchanged. */# Include <stdio. h> # include <stdlib. h ># define MAX 20int main (void) {int set [MAX]; int m, n, position; int I; printf ("Number of input sets :"); scanf ("% d", & m); printf ("input extract element n:"); scanf ("% d", & n); for (I = 0; I <n; I ++) {set [I] = I + 1 ;}for (I = 0; I <n; I ++) {printf ("% d", set [I]);} putchar ('\ n'); position = n-1; while (1) {if (set [n-1] = m) {position --;} else {position = n-1;} set [position] ++; for (I = position + 1; I <n; I ++) {set [I] = set [I-1] + 1 ;}for (I = 0; I <n; I ++) {printf ("% d", set [I]);} putchar ('\ n '); if (set [0]> = m-n + 1) {break ;}} return 0 ;}

 

Related Article

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.