Since ancient time, people has been dreaming of flying in the sky. Eventually, the dream is realized in the 20th century. Nowadays, the airplane becomes a useful vehicle that's 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 is not effective enough till this century.
Since the helicopter rises through the updraft generated by the airscrew, it's very important for it to keep balance. Even for the Late-model helicopters, the loads is required to being distributed evenly, so that the center of gravity of the Helicopter lies just underneath the airscrew. It's one of the most important requirements for it in flight.
Now, your is requested by an airline company to write a program for a passenger transport helicopter. The program could arrange a seat for each passenger automatically so, 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 is designed as the figure below.
Assume the distance of the adjoining seats is 1 unit, and the Airscrew occupies a seat. A seat along with a passenger on it would generate a transverse mome nt and a longitudinal moment.the transverse moment, MvI, is the weight of a passenger in the seat multiplied by the transverse distance from the seat to the Airscrew. the longitudinal moment, MhI, is the weight of a passenger in the seat multiplied by the longitudinal distance. If the transverse moments generated by the passengers on the left is assumed to being positive, the moments by the passenger s on the right would be negative. Also, if the longitudinal moments generated by the passengers in front is assumed to being positive, the moments by the pass Engers on the back would be negative. That's, the moments may counteract and each of the other. The formula below to figure out the composition of moments.
M=
If M = 0, the center of gravity of the helicopter lies just underneath the airscrew.
You is required to arrange the seats of 8 passengers according to their weights to locate the center of gravity underneat H the Airscrew as far as possible. That's, 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 was all 0. And this test case is should not being 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 is 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
Test instructions: A gray machine, eight seats, sitting eight people, a weight per person, the need to make the gray machine balance, even if the above formula to obtain the m as small as possible. MVI refers to the weight of the person I personally multiplied by his relative horizontal displacement, MHI refers to the weight of the person I personally multiplied by his relative vertical displacement; As for the so-called relative horizontal vertical displacement, such as the upper left corner of the person, the relative horizontal displacement is-1, the relative vertical displacement is 1, the upper right corner relative to the horizontal displacement is That's clear.
Because there are only 8 people, the data is very small, even if the whole arrangement is 8! It won't be too big. So can be directly violent calculation.
The only thing that makes you feel a bit valuable is the use of next_permutation in STL. Detailed introduction of self-Baidu.
1#include <iostream>2#include <cstdio>3#include <cstdlib>4#include <cmath>5#include <algorithm>6 using namespacestd;7 Const intINF =0x7fffffff;8 intweight[Ten];9 DoubleSolve ()Ten { One intSUM_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; A intSUM_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; - returnsqrt (sum_mv*sum_mv+sum_mh*sum_mh); - } the - intMain () - { - + while(1) - { + BOOLOK =false; A for(inti =0; I <8; i++) at { -scanf"%d", &weight[i]); - if(Weight[i]) OK =true; - } - if(!ok) Break; - inSort (weight, weight+8); - DoubleAns =INF; to while(Next_permutation (weight, weight+8)) + { -Ans =min (ans, solve ()); the } *printf"%.3lf\n", ans); $ }Panax Notoginseng return 0; -}
"Violence, STL, water" UVa 1523-helicopter