C++ 穷举算法 鸡兔同笼

 1 #include "stdio.h"
 2 int qiongju(int head, int foot, int *chicken, int *rabbit)
 3 {
 4     int re, i, j;
 5     re = 0;
 6     for (i = 0; i <= head; i++)
 7     {
 8         j = head - i;
 9         if (i * 2 + j * 4 == foot)
10         {
11             re = 1;
12             *chicken = i;
13             *rabbit = j;
14         }
15     }
16     return re;
17 }
18 
19 void main()
20 {
21     int chicken, rabbit, head, foot;
22     int re;
23 
24     re = qiongju(head,foot,&chicken,&rabbit);
25 
26     if (re == 1)
27     {
28         printf("鸡有:%d 只,兔子有:%d 只。\n",chicken,rabbit);
29     }
30     else 
31     {
32         printf("无法求解!\n");
33     }
34 }

注意变量 re !!!

猜你喜欢

转载自www.cnblogs.com/zhibei/p/11145694.html