题目描述: 用小于等于n元去买100只鸡,大鸡5元/只,小鸡3元/只,还有1/3元每只的一种小鸡,分别记为x只,y只,z只。编程求解x,y,z所有可能解。
代码
用了ceil函数,确保答案肯定是正确的
#include <cstdio>#include <algorithm>#include <cmath>using namespace std;int main(){int n;while(scanf("%d", &n)!=EOF){int x, y, z; // x,y,z代表个数for(x = 0; x <= 100; x++){for(y = 0; y <= 100 - x; y++){z = 100 - x - y;if(5 * x + 3 * y + ceil(z * 1.0 / 3) <= n){printf("x=%d,y=%d,z=%d\n", x, y, z);} else {break;}}}}}
