解题思路:
1.首先定义变量coincock(可用来买公鸡的钱),coinhen(可用来买母鸡的钱),coinchi(可用来买小鸡的钱);
2.先买公鸡,买后剩余的钱存在coinhen里; 买完后剩余的钱可以买coinhen/3只母鸡;
3.然后买母鸡,买后剩余的钱存在coinchi里; 买完后剩余的钱可以买coinchi*3只小鸡;
4.最后把coinchi全买小鸡;
5.看买的公鸡+母鸡+小鸡是否等于100,等于则输出解;
注意事项:
不可以就用一个变量存每个过程剩余的钱:
for ( int cock = 0; cock <= 12; cock++ ) { coin = 100; coin = coin - (5 * cock); for ( int hen = 0; hen <= (coin / 3); hen++ ) { coin = coin - (3 * hen); if ( (cock + hen + coin * 3 == 100) ) printf( "cock=%d,hen=%d,chicken=%d\n", cock, hen, coin * 3 ); } }
coin买完母鸡后就变了,再次返回买母鸡时,可买的母鸡数不对的;
参考代码:
#include <stdio.h> int main() { int coincock, coinhen, coinchi; for ( int cock = 0; cock <= 12; cock++ ) { coincock = 100; coinhen = coincock - (5 * cock); for ( int hen = 0; hen <= (coinhen / 3); hen++ ) { coinchi = coinhen - (3 * hen); if ( (cock + hen + coinchi * 3 == 100) ) printf( "cock=%d,hen=%d,chicken=%d\n", cock, hen, coinchi * 3 ); } } return(0); }
别忘点赞哦-.-
0.0分
15 人评分
#include <stdio.h> #include <string.h> #include <math.h> int main() { int i,j,k,a,b,c; for(a=0,i=0;i<25;a+=5,i++) { for(b=0,j=0;j<33;b+=3,j++) { for(c=0,k=0;k<300;c++,k+=3) { if(a+b+c==100&&i+j+k==100) { printf("cock=%d,hen=%d,chicken=%d\n",i,j,k); } } } } }
#include<stdio.h> int main() { int cock, hen, n = 0 ,chicken; for (cock = 0; cock < 60; cock++) for (hen = 0; hen < 99; hen++) for (chicken = 0; chicken < 900; chicken++) if (15 * cock + 9 * hen + chicken == 300&&cock+hen+chicken==100) printf("cock=%d,hen=%d,chicken=%d\n", cock, hen, chicken); return 0; }
C语言训练-大、小写问题 (C语言代码)浏览:792 |
WU-陶陶摘苹果2 (C++代码)浏览:1018 |
C语言程序设计教程(第三版)课后习题6.8 (C语言代码)浏览:544 |
C二级辅导-公约公倍 (C语言代码)浏览:537 |
图形输出 (C语言代码)浏览:1019 |
C语言程序设计教程(第三版)课后习题3.7 (C语言代码)浏览:587 |
1074(纳闷了,答案错误,求指教)浏览:384 |
1227题解浏览:582 |
C语言程序设计教程(第三版)课后习题11.3 (C语言代码)浏览:2173 |
C语言程序设计教程(第三版)课后习题5.5 (C语言代码)浏览:517 |