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

一道r组合题目

时间:2014-11-04 16:36:38      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   color   ar   os   for   sp   

题目:

多维数组变一维数组,规则如下:
1. arrA[a,b,c,d,e,f] = arrB[n];
 2. 要求a<b<c<d<e<f<33;
例如:
arrA[ 0, 1, 2, 3, 4, 5] = arrB[0]
 arrA[ 0, 1, 2, 3, 4, 6] = arrB[1]
 arrA[ 0, 1, 2, 3, 4,32] = arrB[27]
 arrA[ 0, 1, 2, 3, 5, 6] = arrB[28]
 arrA[ 0, 1, 2, 3, 5, 7] = arrB[29]
 arrA[ 0, 1, 2, 3, 5,32] = arrB[54]
 arrA[ 0, 1, 2, 3, 6, 7] = arrB[55]
 arrA[ 0, 1, 2, 4, 5, 6] = arrB[406]
 arrA[ 0, 1, 2, 6, 7, 8] = arrB[1135]
 arrA[ 0, 1, 2, 7, 8, 9] = arrB[1460]
 arrA[ 0, 1, 3, 4, 5, 6] = arrB[4060]
// arrA[ 0, 1, 3, 4, 5, 7] = arrB[5138]
 //arrA[ 0, 1, 4, 5, 6, 7] = arrB[7144]
求:1. arrB[854564]=arrA[a,b,c,d,e,f]中的abcdef值
2. arrB[N] = arrA[a,b,c,d,e,f]中的abcdef表达式或者程序

分析:

题目可以看为求0..32中任取6个数且从小到大排列的所有解,每个解中的元素从小到大排列;

arrA[a,b,c,d,e,f]中的数据组合就是上述问题的一个解,arrB[n]中的n是这个解在所有解中的序数;

arrA[x1, x2, x3, x4, x5] = arrB[y]  

其中,x1<x2<x3<x4<x5,  y是这个解在所有解中的序数;

原问题就可以转换为,求 每个解 和 其序数 的对应关系。

 

代码实现:

bubuko.com,布布扣
 1 int _tmain(int argc, _TCHAR* argv[])
 2 {
 3     int a, b, c, d, e, f;
 4     double n = 0;
 5     double cnt = 0;
 6     a = 0; b = 1; c = 2; d = 3; e = 4; f = 5;
 7 
 8     printf("输入n:");
 9     scanf("%lf", &n);
10 
11     while(f<=32 && cnt<n)
12     {
13         if (++f > 32)
14         { 
15             if (++e > 31)
16             {
17                 if (++d > 30)
18                 {
19                     if (++c > 29)
20                     {
21                         if (++b > 28)
22                         {
23                             if (++a > 27)
24                             {
25                                 printf("超出范围!\n");
26                                 break;
27                             }
28                             else
29                             {
30                                 b = a+1; c = b+1; d = c+1; e = d+1; f=e+1;
31                             }
32                         }
33                         else
34                         {
35                             c = b+1; d = c+1; e = d+1; f=e+1;
36                         }
37                     }
38                     else
39                     {
40                         d = c+1; e = d+1; f=e+1;
41                     }
42                 }
43                 else
44                 {
45                     e = d+1; f=e+1;
46                 }
47             }
48             else
49             {
50                 f = e+1;
51             }
52         }
53         cnt++;
54         
55     }
56 
57     printf("cnt:%1f\n", cnt);
58     printf("a:%d b:%d c:%d d:%d e:%d f:%d\n", a, b, c, d, e, f);
59 
60     return 0;
61 }
if...else..
bubuko.com,布布扣
 1 //引自csdn::zhao4zhong1 
 2 #include <stdio.h>
 3 int i,a,b,c,d,e,f;
 4 int main() {
 5     i=0;
 6     for (a=0  ;a<28;a++) {
 7     for (b=a+1;b<29;b++) {
 8     for (c=b+1;c<30;c++) {
 9     for (d=c+1;d<31;d++) {
10     for (e=d+1;e<32;e++) {
11     for (f=e+1;f<33;f++) {
12         printf("arrA[%2d,%2d,%2d,%2d,%2d,%2d]=arrB[%i]\n",a,b,c,d,e,f,i);
13         i++;
14     }
15     }
16     }
17     }
18     }
19     }
20     return 0;
21 }
22 //arrA[ 0, 1, 2, 3, 4, 5]=arrB[0]
23 //arrA[ 0, 1, 2, 3, 4, 6]=arrB[1]
24 //……
25 //arrA[ 0, 1, 2, 3, 4,32]=arrB[27]
26 //arrA[ 0, 1, 2, 3, 5, 6]=arrB[28]
27 //arrA[ 0, 1, 2, 3, 5, 7]=arrB[29]
28 //……
29 //arrA[ 0, 1, 2, 3, 5,32]=arrB[54]
30 //arrA[ 0, 1, 2, 3, 6, 7]=arrB[55]
31 //……
32 //arrA[ 0, 1, 2, 4, 5, 6]=arrB[406]
33 //……
34 //arrA[ 0, 1, 2, 6, 7, 8]=arrB[1135]
35 //……
36 //arrA[ 0, 1, 2, 7, 8, 9]=arrB[1460]
37 //……
38 //arrA[ 0, 1, 3, 4, 5, 6]=arrB[4060]
39 //……
40 //arrA[ 0, 1, 3, 7, 9,10]=arrB[5138]
41 //……
42 //arrA[ 0, 1, 3,16,28,29]=arrB[7144]
43 //……
44 //arrA[ 6,11,15,25,28,29]=arrB[854564]
45 //……
46 //arrA[27,28,29,30,31,32]=arrB[1107567]
for


相关:

如何求一个r组合的编号:http://blog.csdn.net/qq675927952/article/details/6539270

一道r组合题目

标签:style   blog   http   io   color   ar   os   for   sp   

原文地址:http://www.cnblogs.com/91Kesson/p/4073713.html

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