School idol project

来源:互联网 发布:大数据培训视频 编辑:程序博客网 时间:2024/06/04 18:29

Bad news! It is said that School of Software is going to be repealed because of low amount of new student. Many people says that TAs always give assignments which are too difficult to finish. Most high school graduates decide not to choose SS after hearing that.

To attract new students and save our school, some SSers decide to start a school idol project. They plan to set up an idol group which contains 9 members ( it seems that 9 is a popular size of school idol group ).

Now there are N students who wants to join the project, numbered from 0 to N-1. Please write a program to output all the possible combinations of the 9 school idols.

The input contains only one number N.

You should output at most 1000 lines, each line is one of the combinations. You should output them in alphabet order. In each line, numbers should be separated by space. There shouldn’t be any spaces at the end of a line. And there should be an empty line at the end of your output.

If C(N, 9) > 1000, output the first 1000 combinations.

Sample Input

10

Sample Output

0 1 2 3 4 5 6 7 8

0 1 2 3 4 5 6 7 9

0 1 2 3 4 5 6 8 9

0 1 2 3 4 5 7 8 9

0 1 2 3 4 6 7 8 9

0 1 2 3 5 6 7 8 9

0 1 2 4 5 6 7 8 9

0 1 3 4 5 6 7 8 9

0 2 3 4 5 6 7 8 9

1 2 3 4 5 6 7 8 9

For all test cases, 9 <= N <= 10000

(高端做法)

#include <stdio.h>int counter = 0;void dfs(const int index, int *s, const int n) {    if (index == 9) {        int i;        for (i = 0; i < 8; ++i)            printf("%d ", s[i]);        printf("%d\n", s[8]);        ++counter;    } else {        for (s[index] = s[index-1]+1; s[index] < n-8+index && counter < 1000;             ++s[index])            dfs(index+1, s, n);    }}int main() {    int s[9], n;    scanf("%d", &n);    for (s[0] = 0; s[0] < n-8; ++s[0])        dfs(1, s, n);    return 0;}

(低端做法)

#include <stdio.h>int main() {    int a1, a2, a3, a4, a5, a6, a7, a8, a0;    int n;    scanf("%d", &n);    int flag = 0;    for (a0 = 0; a0 < n - 8; a0++) {        for (a1 = a0 + 1; a1 < n - 7; a1++) {            for (a2 = a1 + 1; a2 < n - 6; a2++) {                for (a3 = a2 + 1; a3 < n - 5; a3++) {                    for (a4 = a3 + 1; a4 < n - 4; a4++) {                        for (a5 = a4 + 1; a5 < n - 3; a5++) {                            for (a6 = a5 + 1; a6 < n - 2; a6++) {                                for (a7 = a6 + 1; a7 < n - 1; a7++) {                                    for (a8 = a7 + 1; a8 < n; a8++) {                                        printf("%d ", a0);                                        printf("%d ", a1);                                        printf("%d ", a2);                                        printf("%d ", a3);                                        printf("%d ", a4);                                        printf("%d ", a5);                                        printf("%d ", a6);                                        printf("%d ", a7);                                        printf("%d\n", a8);                                        flag++;                                        if (flag >= 1000) {                                            return 0;                                        }                                    }                                }                            }                        }                    }                }            }        }    }    return 0;}
0 0