Chinaunix首页 | 论坛 | 博客
  • 博客访问: 45039
  • 博文数量: 6
  • 博客积分: 202
  • 博客等级: 入伍新兵
  • 技术积分: 90
  • 用 户 组: 普通用户
  • 注册时间: 2009-09-15 16:52
文章分类

全部博文(6)

文章存档

2013年(1)

2011年(5)

我的朋友

分类: C/C++

2011-06-15 09:12:40

  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. void func(int eggs, int *global_baskets, int start_basket, int baskets, int max_eggs_per_basket)
  4. {
  5.   int i;
  6.   int max_eggs_first_basket;
  7.   int min_eggs_first_basket;

  8.   if (eggs == 0) {
  9.     for (i = 0; i < (start_basket-1); i++) {
  10.       printf("%d ", global_baskets[i]);
  11.     }
  12.     printf("%d\n", global_baskets[start_basket-1]);
  13.   }
  14.   else if (eggs == 1) {
  15.     for (i = 0; i < start_basket; i++) {
  16.       printf("%d ", global_baskets[i]);
  17.     }
  18.     printf("1\n");
  19.   }
  20.   else if (baskets == 1) {
  21.     for (i = 0; i < start_basket; i++) {
  22.       printf("%d ", global_baskets[i]);
  23.     }
  24.     printf("%d\n", eggs);
  25.   }
  26.   else {
  27.     max_eggs_first_basket = (eggs > max_eggs_per_basket)?max_eggs_per_basket:eggs;
  28.     min_eggs_first_basket = (eggs+baskets-1)/baskets;

  29.     for (i = max_eggs_first_basket;i >= min_eggs_first_basket; i--) {
  30.       global_baskets[start_basket] = i;
  31.       func(eggs-i, global_baskets, start_basket+1, baskets-1, i);
  32.     }
  33.   }
  34. }

  35. int main(int argc, char *argv[])
  36. {
  37.   int M = 0;
  38.   int N = 0;
  39.   int *global_baskets = NULL;
  40.   
  41.   if (argc != 3) {
  42.     printf("Usage: eggs EGGS BASKETS\n");
  43.     return -1;
  44.   }
  45.   
  46.   M = atoi(argv[1]);
  47.   N = atoi(argv[2]);
  48.   
  49.   global_baskets = malloc(sizeof(int) * N);
  50.   
  51.   func(M, global_baskets, 0, N, M);
  52.   
  53.   free(global_baskets);
  54.   
  55.   return 0;
  56. }
阅读(2445) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~