设计一个对下列问题的求解算法,并给出该算法的时间 复杂度和空间复杂度 今有36块砖,36人搬,男搬4,女搬3,两个小孩抬一砖,要求一次搬完,问男女小孩各需若干 提示:用穷举法求满足方程的解: 4.0*m+3.0*w+0.5*c=36.0 //源程序如下.如有不妥之处不要见怪.呵呵!!! #include<iostream.h>#include<iomanip.h>void main(){ int m,w,c; cout<<" 男 女 小孩"<<endl; for(m=0;m<=8;m++) for(w=0;w<=11;w++){ c=36-m-w; if((4.0*m+3.0*w+0.5*c==36.0 )&&(c%3==0)) cout<<setw(6)<<m<<setw(10)<<w<<setw(10)<<c<<endl; }} 另一种方法: #include <iostream.h> Bz(int x,int y,float z ,float w ) { int w,m,c; for ( m=0;m<9;m++) for( w=0;w<12;w++) { c=36-m-w ; if (x*m+y*w+z*c = =w ) { cout<<”man is”<<m<<endl; cout<<”woman is”<<w<<endl; cout<<”childs is”<<c<<endl; } main( ) { bz( 4, 3, 0.5, 36.0); } t(n)=9*12=108=O(108) =O(1) s(n)=4=O(4)

评论