一道组合数问题
2011-05-15 22:05
225 查看
#include <stdio.h>
void CombNumber(int a[], int m, int n, int h, int *count);
int main()
{
int m, n;
int count = 0;
static int Res[100];
printf("Please input the number m and n :/n");
scanf("%d%d", &m, &n);
printf("The combination number is :/n");
CombNumber(Res, m, n, 0, &count);
printf("The res has %d ways:/n", count);
puts("");
return 0;
}
void CombNumber(int Res[], int m, int n, int h, int *count)
{
int i = 0, j = 0, k = 0;
if(n == h) //条件成立打印结果
{
(*count)++;
for(i = 0; i <= h; ++i)
{
printf("%d", Res[i]);
}
printf("/b/t");
return;
}
for(j = 1; j <= m; ++j)
{
for(k = 0; k < h; ++k)
{
if(j == Res[k] /*|| Res[k] > j*/) //判断当前数字是否已在结果数组中
{
break;
}
}
if(k >= h)
{
Res[h] = j;
CombNumber(Res, m, n, h + 1, count);
}
}
}
/*
Please input the number m and n :
5 3
The combination number is :
123 124 125 132 134 135 142 143 145 152
153 154 213 214 215 231 234 235 241 243
245 251 253 254 312 314 315 321 324 325
341 342 345 351 352 354 412 413 415 421
423 425 431 432 435 451 452 453 512 513
514 521 523 524 531 532 534 541 542 543
The res has 60 ways:
Press any key to continue
*/
void CombNumber(int a[], int m, int n, int h, int *count);
int main()
{
int m, n;
int count = 0;
static int Res[100];
printf("Please input the number m and n :/n");
scanf("%d%d", &m, &n);
printf("The combination number is :/n");
CombNumber(Res, m, n, 0, &count);
printf("The res has %d ways:/n", count);
puts("");
return 0;
}
void CombNumber(int Res[], int m, int n, int h, int *count)
{
int i = 0, j = 0, k = 0;
if(n == h) //条件成立打印结果
{
(*count)++;
for(i = 0; i <= h; ++i)
{
printf("%d", Res[i]);
}
printf("/b/t");
return;
}
for(j = 1; j <= m; ++j)
{
for(k = 0; k < h; ++k)
{
if(j == Res[k] /*|| Res[k] > j*/) //判断当前数字是否已在结果数组中
{
break;
}
}
if(k >= h)
{
Res[h] = j;
CombNumber(Res, m, n, h + 1, count);
}
}
}
/*
Please input the number m and n :
5 3
The combination number is :
123 124 125 132 134 135 142 143 145 152
153 154 213 214 215 231 234 235 241 243
245 251 253 254 312 314 315 321 324 325
341 342 345 351 352 354 412 413 415 421
423 425 431 432 435 451 452 453 512 513
514 521 523 524 531 532 534 541 542 543
The res has 60 ways:
Press any key to continue
*/
相关文章推荐
- 一道组合数问题--出自 曹钦翔_wc2012组合计数与动态规划
- NOIP 2016 Senior 4 - 组合数问题
- 由一道淘宝面试题到False sharing问题
- 一道面试题:赛马问题
- 求解一道有关N-Queen的问题
- NYOJ 组合数问题
- 一道C语言编程比赛题(时钟追赶问题)
- 洛谷 P2822 组合数问题
- 每天一道LeetCode--141.Linked List Cycle(链表环问题)
- 【NOIP2016】组合数问题
- 一道数学问题的程序解答
- 由一道逻辑推理题衍生的对于实际问题求解的一般思路(续)
- 一道经典面试题,字符数组排序问题(字符包含a-z、A-Z、0-9.)
- 展讯的一道笔试Java题(关于静态初始化问题)
- C语言面试那些事儿──一道指针与数组问题:a+1 and &a+1
- 三进制与一道经典的砝码问题
- n取的r的组合数问题
- 一道C语言面试题——邮票组合问题
- 一道面试问题学生们的多种解答汇总
- 华为的另一道面试题,有关union的sizeof()的问题