日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

uva 10883——Supermean

發布時間:2023/11/30 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 uva 10883——Supermean 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Do you know how to compute the mean (or average) of?n?numbers? Well, that's not good enough for me. I want the supermean! "What's a supermean," you ask? I'll tell you. List the?n?given numbers in non-decreasing order. Now compute the average of each pair of adjacent numbers. This will give you?n?- 1 numbers listed in non-decreasing order. Repeat this process on the new list of numbers until you are left with just one number - the supermean. I tried writing a program to do this, but it's too slow. :-( Can you help me?

Input
The first line of input gives the number of cases,?N.?N?test cases follow. Each one starts with a line containing?n?(0<<b>n<=50000). The next line will contain the?ninput numbers, each one between -1000 and 1000, in non-decreasing order.

Output
For each test case, output one line containing "Case #x:" followed by the supermean, rounded to 3 fractional digits.

?

Sample Input

4

1

10.4

2

1.0 2.2

3

1 2 3

5

1 2 3 4 5

?

Sample Output

Case #1: 10.400

Case #2: 1.600

Case #3: 2.000

Case #4: 3.000

?題意:給出n個數字, 要求你求出它們的supermean, supermean的定義是: n個數先兩兩相鄰求平均值, 那么得到n-1個數, 已知循環做這件事,?直到剩下的數字只有1, 那么這數就是supermean.


思路:該題在劉汝佳的紫書上《入門經典第二版》上有涉及(具體320頁),沒有書的也可查看我博客中的uva 1635題,和該題類似,比如假設5個數a1,a2,a3,a4,a5,最后退出的結果是a1+4a2+6a3+4a4+a5,聰明的小伙伴或許已經想到楊輝三角,就是楊輝三角,二項式系數c40到c44,然后去求即可,打不了二維表,對于每個n,打一個一維表,最后一步關鍵的高精度處理,這里采用取對數用double保存;

code:

#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
using namespace std;

const int N=50011;
double f[N];

void my_way(int n) //對于每個n,打一個一維表
{
//f[0]=1;
f[0]=f[n]=0;
for (int i=1;i<=n/2;i++)
f[n-i]=f[i]=f[i-1]+log10(n-i+1)-log10(i); //高精度處理1
}
int main()
{
int n,T,ca=1;
double ans,a;
scanf("%d",&T);
while (T--)
{
scanf("%d",&n);
ans=0;
my_way(n-1);
for (int i=0;i<n;i++)
{
scanf("%lf",&a);
ans+=1.0*pow(10,f[i]-(n-1)*log10(2))*a; //高精度處理2
}
printf("Case #%d: %.3lf\n",ca++,ans);
}
}


總結

以上是生活随笔為你收集整理的uva 10883——Supermean的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。