习题:有100个学生种100棵树,其中高中生每人种3棵树,初中生每人种1棵树,小学生每3人种1棵树,问高中生、初中生、小学生各有多少人?
作者:互联网
个人答案:
#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; }
标签:std,include,int,namespace,初中生,高中生,using,100,main 来源: https://blog.csdn.net/weixin_56463218/article/details/120931959