0
点赞
收藏
分享

微信扫一扫

C语言——鸡兔同笼问题

没注释的源代码

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

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) 
{
  int tou = 10;                      
  int jiao = 36;                      
  int ji_jiao = tou*2;                 
  int tu_jiao = jiao - ji_jiao;       
  int tu = tu_jiao / 2;             
  int ji = tou - tu;                  
  printf("鸡的数量:%d",ji);           
  printf("兔的数量:%d",tu);          
  return 0;
}

注释的源代码

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

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) 
{
  int tou = 10;                        // 鸡头和兔头一共10只
  int jiao = 36;                       // 鸡脚和兔脚一共36只
  int ji_jiao = tou*2;                 // 全部为鸡时(即兔抬起两只脚时,视为鸡),鸡的脚是头的两倍
  int tu_jiao = jiao - ji_jiao;       // 抬起来的两只脚就是兔子的脚
  int tu = tu_jiao / 2;               // 兔等于抬起来的两只脚的一半,这里用//(整除)而不是/(除)
  int ji = tou - tu;                   // 鸡的数量是总数减去兔的数量
  printf("鸡的数量:%d",ji);           // 输出鸡的数量
  printf("兔的数量:%d",tu);           // 输出兔的数量
  return 0;
}

举报

相关推荐

0 条评论