Sicily 1336. Power Mean

来源:互联网 发布:在线域名生成器短链接 编辑:程序博客网 时间:2024/06/05 09:09

1336. Power Mean

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

Description

The power mean P of N numbers Xi is defined as

$\displaystyle \left(\vphantom{\frac{1}{n} \sum^{N}_{i=1}(X^{P}_{i}) }\right.$$\displaystyle {\frac{{1}}{{n}}}$$\displaystyle \sum^{{N}}_{{i=1}}$(XPi)$\displaystyle \left.\vphantom{\frac{1}{n} \sum^{N}_{i=1}(X^{P}_{i}) }\right)^{{1/P}}_{}$

where P is a real number and the Xi are real numbers $ \ge$ 0 .

P = 1 is the familiar average or mean, P = - 1 is the harmonic mean, P = 2 is the quadratic or root mean square. P = 0 is the geometric mean, but this requires taking limits or using the more familiar product representation.

Your team is to write a program that will compute the power mean for given values of Xi .

Input

Input to your program will be a series of test cases, one test case per line. Each line has a series of real numbers separated by whitespace. The first number is P , . 5$ \le$|P|$ \le$10 . The rest of the numbers are the Xi , 0$ \le$Xi$ \le$100 , 1$ \le$i$ \le$20 .

Output

For each test case, print the power mean P starting in the first column, rounding to 3 places after the decimalpoint. Add a leading zero only if the result is < 1 , as in the sample output.

Sample Input

-1.0 4.3 10.18 37.0 5.0 83.5 64.1 5 72.62 1.0 4.3 10.18 37.0 5.0 83.5 64.1 5.0 72.62 0.5 4.3 10.18 37.0 5.0 83.5 64.1 5.0 72.62 -0.5 4.3 10.18 37.0 5.0 83.5 64.1 5.0 72.62 10.0 4.3 10.18 37.0 5.0 83.5 64.1 5.0 72.62 4.0 0.0 5.0 0.0 1.4 47.7 9.13 9.8 2.83 61.31 -3.7 2.83 1.1 2.11 6.90 41.0 7.7 0.01 31.6 83.73

Sample Output

10.010 35.213 26.891 13.149 69.727 0.000 43.683 0.018
// Problem#: 1336// Submission#: 3298611// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/// All Copyright reserved by Informatic Lab of Sun Yat-sen University#include <iostream>#include <vector>#include <algorithm>#include <stdio.h>#include <math.h>#include <string.h>using namespace std;double p;int n;double x[105];char text[10000];double char_to_double(char s[]) {    double ans;    sscanf(s, "%lf", &ans);    return ans;}void init() {    int length = strlen(text);    for (int i = length - 1; i >= 0; i--) {        if (text[i] == ' ') text[i] = '\0';        else break;    }    char temp[10000];    bool lastIsBlank = false;    int i = 0;    int j = 0;    for (; text[i] != '\0'; i++) {        if (!lastIsBlank || (lastIsBlank && text[i] != ' ')) temp[j++] = text[i];        if (text[i] == ' ') lastIsBlank = true;        else lastIsBlank = false;    }    temp[j] = '\0';    while (j >= 0) {        text[j] = temp[j];        j--;    }}void readin() {    init();    n = 0;    bool isP = true;    char s[15];    int startPos = 0;    for (int i = 0; 1; i++) {        if (text[i] == ' ' || text[i] == '\0') {            s[i - startPos] = '\0';            if (isP) {                p = char_to_double(s);                isP = false;            } else {                x[n++] = char_to_double(s);            }            if (text[i] == '\0') break;            startPos = i + 1;        } else {            s[i - startPos] = text[i];        }    }}int main() {    //std::cout.sync_with_stdio(false);    while (gets(text)) {        double ans = 0;        readin();        for (int i = 0; i < n; i++) {            ans += pow(x[i], p);        }        printf("%.3lf\n", pow((ans / n), 1 / p));    }    return 0;}                                 



0 0
原创粉丝点击