Since ancient time, people have been dreaming of flying in the sky. Eventually, the dream was realized in the 20th century. Nowadays, the airplane becomes a useful vehicle that is used frequently in our daily life.
But before the dream came true, a large number of people had tried to design the aircrafts. One of those aircrafts, which is called ``helicopter" in modern time, can be traced back to the blueprint of the aircraft designed by Leonardo da Vinci. But the helicopter was not effective enough till this century.
Since the helicopter rises through the updraft generated by the airscrew, it is very important for it to keep balance. Even for the late-model helicopters, the loads are required to be distributed evenly, so that the center of gravity of the helicopter lies just underneath the airscrew. It is one of the most important requirements for it in flight.
Now, you are requested by an airline company to write a program for a passenger transport helicopter. The program may arrange a seat for each passenger automatically so that the center of gravity of the helicopter should be located underneath the airscrew as dose as possible. The seats in the helicopter and the airscrew are designed as the figure below.
You may assume the distance of the adjoining seats is 1 unit, and the airscrew occupies a seat. A seat along with a passenger on it will generate a transverse mome nt and a longitudinal moment. The transverse moment,Mvi, is the weight of a passenger on the seat multiplied by the transverse distance from the seat to the airscrew. The longitudinal moment, Mhi, is the weight of a passenger on the seat multiplied by the longitudinal distance. If the transverse moments generated by the passengers on the left are assumed to be positive, the moments by the passengers on the right will be negative. Also, if the longitudinal moments generated by the passengers in front are assumed to be positive, the moments by the passengers on the back will be negative. That is, the moments may counteract with each other. You may use the formula below to figure out the composition of moments.
If M = 0, the center of gravity of the helicopter lies just underneath the airscrew.
You are required to arrange the seats of 8 passengers according to their weights to locate the center of gravity underneath the airscrew as far as possible. That is, the value of M should be minimum.
Input
The input file may contain several test cases. Each test case consists of eight integers in lines, which represent the weights of those passengers. The end of input is signified by the test case in which the weights are all 0. And this test case should not be processed.
Output
The output for each test case should include a line contains a real number which tells the composition of moments, M, in the optimal arrangement. The output of the composition of moments should be accurate to 3 decimal places. And you should not print any more white spaces or blank lines in the output.
Sample Input
1 2 3 4 5 6 7 8 0 0 0 0 0 0 0 0
Sampple Output
0.000
题意:一架灰机,八个座位,坐着八个人,每人一个重量,要求使灰机平衡,即使上面的公式求得的M尽量小。式中Mvi指第i个人的重量乘以他的相对水平位移,Mhi指第i个人的重量乘以他的相对垂直位移;至于所谓的相对水平垂直位移,比如左上角的人,相对水平位移是-1,相对垂直位移是-1,右上角相对水平位移是1,相对垂直位移是-1,这样明白了吧。
因为只有8个人,数据很小,即使全排列8!也不会很大。所以可以直接暴力计算。
这个题唯一让自己觉得有点价值的就是STL中next_permutation的使用。详细的介绍自行百度。
1 #include<iostream> 2 #include<cstdio> 3 #include<cstdlib> 4 #include<cmath> 5 #include<algorithm> 6 using namespace std; 7 const int INF = 0x7fffffff; 8 int weight[10]; 9 double solve() 10 { 11 int sum_Mv = weight[0]*(-1) + weight[1]*0 + weight[2]*1 + weight[3]*(-1) + weight[4]*1 + weight[5]*(-1) + weight[6]*0 + weight[7]*1; 12 int sum_Mh = weight[0]*(-1) + weight[1]*(-1) + weight[2]*(-1) + weight[3]*0 + weight[4]*0 + weight[5]*1 + weight[6]*1 + weight[7]*1; 13 return sqrt(sum_Mv*sum_Mv+sum_Mh*sum_Mh); 14 } 15 16 int main() 17 { 18 19 while(1) 20 { 21 bool ok = false; 22 for(int i = 0; i < 8; i++) 23 { 24 scanf("%d", &weight[i]); 25 if(weight[i]) ok = true; 26 } 27 if(!ok) break; 28 29 sort(weight, weight+8); 30 double ans = INF; 31 while(next_permutation(weight, weight+8)) 32 { 33 ans = min(ans, solve()); 34 } 35 printf("%.3lf ", ans); 36 } 37 return 0; 38 }