码迷,mamicode.com
首页 > 其他好文 > 详细

School idol project

时间:2015-12-21 23:14:51      阅读:249      评论:0      收藏:0      [点我收藏+]

标签:

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

 

一开始,我想到的就是九重循环(hhhhh),但是还是打不出来,因为一直在纠结怎样退出循环的问题,以及怎样能使打印正常,然后看了看正确的九重,发现了return 0作用:

return 在main函数里面是程序退出的语句,当函数中遇到return 0;则会终止该函数

所以在大于1000时候,就可以通过标记退出mmain函数,然后循环的思路也就是这样,不要想得太复杂;;;;
还有一个很重要的原来也不会,就是one,two……nine的范围,我一开始使他们都 < n,但是这样是错误的,而需要不断递减最小值,这点需要留意;;;

当然,并不提倡这种做法,如果需要排列100个,那就需要100重循环吗????掌握递归,听说是与深度优先搜索差不多,我再理解一下



我的
 1 #include<stdio.h>
 2 int main() {
 3     int n, a[10000], i, pan = 0;
 4     scanf("%d", &n);
 5     for (i = 0; i < n; i++) {
 6         a[i] = i;
 7     }
 8     int one, two, three, four, five, six, seven, eight, nine;
 9     for (one = 0; one < n - 8; one++) {
10         for (two = one + 1; two < n - 7; two++) {
11             for (three = two + 1; three < n - 6; three++) {
12                 for (four = three + 1; four < n - 5; four++) {
13                     for (five = four + 1; five < n - 4; five++) {
14                         for (six = five + 1; six < n - 3; six++) {
15                             for (seven = six + 1; seven < n - 2; seven++) {
16                             for (eight = seven + 1; eight < n - 1; eight++) {
17                                     for (nine = eight + 1; nine < n; nine++) {
18                                         printf("%d ", a[one]);
19                                         printf("%d ", a[two]);
20                                         printf("%d ", a[three]);
21                                         printf("%d ", a[four]);
22                                         printf("%d ", a[five]);
23                                         printf("%d ", a[six]);
24                                         printf("%d ", a[seven]);
25                                         printf("%d ", a[eight]);
26                                         printf("%d\n", a[nine]);
27                                         pan++;
28                                         if (pan >= 1000)
29                                             return 0;
30                                     }
31                                 }
32                             }
33                         }
34                     }
35                 }
36             }
37         }
38     }
39 }

 


标答



School idol project

标签:

原文地址:http://www.cnblogs.com/-lyric/p/5064982.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!