今天写了一道超简单的百鸡问题:
#include <iostream>
using namespace std;
int main(){
int n;
while(cin>>n){
int max_x = 100<(n/5)?100:(n/5); //因为x不可能大于100,也不可能大于花光所有钱所能买的最多的x鸡。
for(int x=0; x<max_x; x++){
for(int y=0; y<100-x; y++){ //这里要注意,x+y<=100。
int z = 100 - x - y;
if(15*x + 9*y + z <= 3*n){ //这里有一个技巧,把不等式乘以3,避免z/3为浮点数造成的精度丢失。
cout<<"x="<<x<<",y="<<y<<",z="<<z<<endl;
}
}
}
}
return 0;
}