3703 - C++作业2-4:搬砖问题
时间限制 : 2 秒
内存限制 : 30 MB
现有n块砖,要由n人一次搬完,假定男人一次可以搬4块,女人一次可以搬3块,两个小孩搬1块,计算这n人中男人、女人和小孩的人数。
程序运行结果如下:
59
men2
women9
children48
men7
women2
children50
</p>
<p>
<span style="font-size:14px;color:#000000;"></span>
</p>
<p>
<span style="font-size:14px;color:#000000;">如果没有满足的情况,显示提示信息“no result!”</span>
</p>
<p>
<span style="font-size:14px;color:#000000;">程序运行结果如下:</span>
</p>
<p>
<span style="font-size:14px;color:#000000;">1</span>
</p>
<p>
<span style="font-size:14px;color:#000000;">no result!</span>
</p>
题目输入
表示人数的整型数
题目输出
所有满足条件的男人、女人和孩子的人数或者提示信息“no result!”
输入/输出样例
输入格式
59
输出格式
men2 women9 children48 men7 women2 children50
C语言解答
//现有n块砖,要由n人一次搬完,假定男人一次可以搬4块,女人一次可以搬3块,两个小孩搬1块 #include <stdio.h> int main() { int n; scanf("%d",&n); int men; int woman; int children; int flag = 0; for( men=1; men<=n/4; men++ ){ for( woman=1;woman<=n/3; woman++ ){ for( children=2; children<=2*n; children+=2 ){ if( men+woman+children==n&&men*4+woman*3+children/2==n ){ printf("men%d\nwomen%d\nchildren%d\n",men,woman,children); flag = 1; } } } } if( flag != 1 ){ printf("no result!"); } return 0; }
C++解答
#include <iostream> using namespace std; int main(){ int x,y,z,n,c=0; cin>>n; for(x=1;x<n;x++){ for(y=1;y<n;y++){ for(z=1;z<n;z++){ if(((double)n==4*(double)x+3*(double)y+(double)z/2)&&(n==x+y+z)){ cout<<"men"<<x<<endl; cout<<"women"<<y<<endl; cout<<"children"<<z<<endl; c=1; } } } } if(c==0){ cout<<"no result!"; } return 0; }