Chinaunix首页 | 论坛 | 博客
  • 博客访问: 768361
  • 博文数量: 199
  • 博客积分: 3584
  • 博客等级: 中校
  • 技术积分: 2193
  • 用 户 组: 普通用户
  • 注册时间: 2008-05-12 21:18
文章分类

全部博文(199)

文章存档

2018年(6)

2013年(14)

2012年(30)

2011年(28)

2010年(24)

2009年(86)

2008年(11)

分类: C/C++

2012-05-10 15:52:56

网上看到的题目:有12苹果,其中有1个与其余的11个都不同,可能比其它的重,也可能比其他的轻。现在给出一个没有砝码的天平,要求称量3次找出这个苹果,并且判断其轻重情况!
我测试的情况是:称量3次是可以找到这个苹果。

点击(此处)折叠或打开

  1. #include <stdio.h>

  2. #define N 12

  3. int fapp(int apple[]);

  4. int main(int argc, char *argv[])
  5. {
  6.         int index = 0;
  7.         int apple[N] = {0};

  8.         for (index = 0; index < N; index++) {
  9.                 for (int i = 0; i < N; i++) {
  10.                         apple[i] = 5;
  11.                 }
  12.                 apple[index] = 1;
  13.                 fapp(apple);
  14.                 apple[index] = 9;
  15.                 fapp(apple);
  16.                 printf("End test-%d\n\n", index);
  17.         }
  18. }

  19. int fapp(int apple[])
  20. {
  21.         int index = 0;
  22.         int partA, partB, partC;

  23.         /* partition the apples to three parts averagely */
  24.         partA = apple[0] + apple[1] + apple[2] + apple[3];
  25.         partB = apple[4] + apple[5] + apple[6] + apple[7];
  26.         partC = apple[8] + apple[9] + apple[10] + apple[11];
  27.         
  28.         if (partA == partB) {/* weigh A&B for 1st time */
  29.                 /* The unusual apple in partC, get 3 apples from partC, weight for 2nd time */
  30.                 if (apple[8] + apple[9] + apple[10] == apple[0] + apple[1] + apple[2]) {
  31.                         /* unusual apple is last, weight for 3rd time */
  32.                         if (apple[8] > apple[11]) {
  33.                                 printf("The last apple is unusual, weight = %d, light.\n", apple[11]);
  34.                         } else {
  35.                 printf("The last apple is unusual, weight = %d, weighter.\n", apple[11]);
  36.             }
  37.                 } else if (apple[8] + apple[9] + apple[10] > apple[0] + apple[1] + apple[2]) {
  38.                         if (apple[8] == apple[10]) {
  39.                                 printf("The apple 9 is unusual, weighter, weight = %d\n", apple[9]);
  40.                         } else if (apple[8] > apple[10]) {
  41.                                 printf("The apple 8 is unusual, weighter, weight = %d\n", apple[8]);
  42.                         } else {
  43.                 printf("The apple 10 is unusual, weighter, weight = %d\n", apple[10]);
  44.             }
  45.                 } else { /* apple[8] + apple[9] + apple[10] < apple[0] + apple[1] + apple[2] */
  46.                         if (apple[8] == apple[10]) {
  47.                                 printf("The apple 9 is unusual, light, weight = %d\n", apple[9]);
  48.                         } else if (apple[8] > apple[10]) {
  49.                                 printf("The apple 10 is unusual, light, weight = %d\n", apple[10]);
  50.                         } else {
  51.                 printf("The apple 8 is unusual, light, weight = %d\n", apple[8]);
  52.             }
  53.                 }
  54.         } else if (partA > partB) {
  55.                 /* The unusual apple in partA or partB, all partC apples is usual */
  56.                 /* partition partA/B to 3 parts, a0,a1,b0; a2,a3,b1; b2,b3 */
  57.                 if (apple[0]+apple[1]+apple[4] == apple[2]+apple[3]+apple[5]) { /* weight 2nd time */
  58.                         /* unusual apple is one of b2,b3 */
  59.                         if (apple[6] == apple[8]) { /* weight 3rd time */
  60.                                 printf("The apple 7 is unusual, weight = %d, light than others\n", apple[7]);
  61.                         } else {
  62.                                 printf("The apple 6 is unusual, weight = %d, light than others\n", apple[6]);
  63.                         }
  64.                 } else if (apple[0]+apple[1]+apple[4] > apple[2]+apple[3]+apple[5]) {
  65.                         }
  66.                 } else if (apple[0]+apple[1]+apple[4] > apple[2]+apple[3]+apple[5]) {
  67.                         /* if unusual apple is weighter, then is one of a0,a1 */
  68.                         /* if unusual apple is light, then is b1 */
  69.                         if (apple[0] == apple[1]) {
  70.                                 printf("The apple 5 is unusual, weight = %d, light than others\n", apple[5]);
  71.                         } else if (apple[0] > apple[1]) {
  72.                                 printf("The apple 0 is unusual, weight = %d, weighter than others\n", apple[0]);
  73.                         } else {
  74.                                 printf("The apple 1 is unusual, weight = %d, weighter than others\n", apple[1]);
  75.                         }
  76.                 } else { /* apple[0]+apple[1]+apple[4] < apple[2]+apple[3]+apple[5] */
  77.                         /* if unusual apple is weighter, then is one of a2,a3 */
  78.                         /* if unusual apple is light, then is b0 */
  79.                         if (apple[2] == apple[3]) {
  80.                                 printf("The apple 4 is unusual, weight = %d, light than others\n", apple[4]);
  81.                         } else if (apple[2] > apple[3]) {
  82.                                 printf("The apple 2 is unusual, weight = %d, weighter than others\n", apple[2]);
  83.                         } else {
  84.                                 printf("The apple 3 is unusual, weight = %d, weighter than others\n", apple[3]);
  85.                         }
  86.                 }
  87.         } else { /* partA < partB */
  88.                 /* The unusual apple in partA or partB, all partC apples is usual */
  89.                 /* partition partA/B to 3 parts, a0,a1,b0; a2,a3,b1; b2,b3 */
  90.                 if (apple[0]+apple[1]+apple[4] == apple[2]+apple[3]+apple[5]) { /* weight 2nd time */
  91.                         /* unusual apple is one of b2,b3 */
  92.                         if (apple[6] == apple[8]) { /* weight 3rd time */
  93.                                 printf("The apple 7 is unusual, weight = %d, weighter than others\n", apple[7]);
  94.                         } else {
  95.                                 printf("The apple 6 is unusual, weight = %d, weighter than others\n", apple[6]);
  96.                         }
  97.                 } else if (apple[0]+apple[1]+apple[4] > apple[2]+apple[3]+apple[5]) {
  98.                         /* if unusual apple is light, then is one of a2,a3 */
  99.                         /* if unusual apple is weight, then is b0 */
  100.                         if (apple[2] == apple[3]) {
  101.                                 printf("The apple 4 is unusual, weight = %d, weighter than others\n", apple[4]);
  102.                         } else if (apple[2] > apple[3]) {
  103.                                 printf("The apple 3 is unusual, weight = %d, light than others\n", apple[3]);
  104.                         } else {
  105.                                 printf("The apple 2 is unusual, weight = %d, light than others\n", apple[2]);
  106.                         }
  107.                 } else { /* apple[0]+apple[1]+apple[4] < apple[2]+apple[3]+apple[5] */
  108.                         /* if unusual apple is light, then is one of a0,a1 */
  109.                         /* if unusual apple is weight, then is b1 */
  110.                         /* if unusual apple is light, then is one of a0,a1 */
  111.                         /* if unusual apple is weight, then is b1 */
  112.                         if (apple[0] == apple[1]) {
  113.                                 printf("The apple 5 is unusual, weight = %d, weighter than others\n", apple[5]);
  114.                         } else if (apple[0] > apple[1]) {
  115.                                 printf("The apple 1 is unusual, weight = %d, light than others\n", apple[1]);
  116.                         } else {
  117.                                 printf("The apple 0 is unusual, weight = %d, light than others\n", apple[0]);
  118.                         }
  119.                 }
  120.         }

  121. }

阅读(853) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~