个人答案:
#include <iostream> using namespace std; int main() { int x, y, z; for(x = 0; x <= 100; x++) { for(y = 0; y <= 100; y++) { for(z = 0; z <= 100; z++) { if((x + y + z == 100) && (3 * x + 2 * y + z / 3.0 == 100.0)) { cout << x << ' ' << y << ' ' << z << ' ' << endl; } } } } return 0; }
结果:
参考答案:
#include <iostream> using namespace std; int main() { int x, y, z; for (x = 1; x <= 100; x++) for (y = 1; y <= 100; y++) for (z = 1; z <= 100; z++) if (x+y+z==100 && 3*x +1*y+z/3==100 && z%3==0) { cout << "高中生学生人数为:"<< x <<" " << "初中生学生人数为:"<< y << " " << "小学生人数为:"<< (100 - x - y) << endl; cout << "高中生种树:"<<3 * x << " " << "初中生种树:"<< 1 * y << " " << "小学生种树:"<< (100-x -y) / 3 << endl; } return 0; }