uva 1523

来源:互联网 发布:sharpdesk扫描软件下载 编辑:程序博客网 时间:2024/06/06 18:24

题意:有8个数字全排列后带入公式计算,选出最小值。

题解:水题。

#include <stdio.h>#include <math.h>#include <algorithm>using namespace std;int flag[8][2] = {{-1, -1}, {-1, 0}, {1, 0}, {1, 1}, {0, -1}, {0, 1}, {-1, 1}, {1, -1}};double arr[8];int main() {while (1) {double sum = 0;for (int i = 0; i < 8; i++) {scanf("%lf", &arr[i]);sum += arr[i];}if (fabs(sum - 0) < 1e-9)break;sort(arr, arr + 8);double minn = 1 << 30;do {double temp1 = 0, temp2 = 0;for (int i = 0; i < 8; i++) {temp1 = temp1 + arr[i] * flag[i][0];temp2 = temp2 + arr[i] * flag[i][1];}double temp = sqrt(temp1 * temp1 + temp2 * temp2);minn = min(minn, temp);} while (next_permutation(arr, arr + 8));printf("%.3lf\n", minn);}return 0;}


0 0