NC207571. SumoandHisFollowers
描述
输入描述
The first line contains — the number of test cases.
For each test case, the first line contains a single integer , the number of people waiting for asking a question.
The next line contains n integers , the time of people required to ask a question.
输出描述
For each of the test cases, output a single integer, the minimun average waiting time of n people (accurate to two decimal places).
示例1
输入:
2 1 5 10 56 12 1 99 1000 234 33 55 99 812
输出:
0.00 291.90
C++14(g++5.4) 解法, 执行用时: 140ms, 内存消耗: 876K, 提交时间: 2020-06-06 13:52:29
#include<bits/stdc++.h> using namespace std; int t, n, a[123456]; int main() { for(cin >> t; t--; ) { cin >> n; for(int i = 0; i < n; i++) cin >> a[i]; sort(a, a+n); double sum = 0, s = 0; for(int i = 0; i < n-1; i++) s += a[i], sum += s; printf("%.2f\n", sum / n); } }
C++11(clang++ 3.9) 解法, 执行用时: 132ms, 内存消耗: 996K, 提交时间: 2020-06-06 20:38:34
#include<bits/stdc++.h> using namespace std; int main(){ int t; cin>>t; while(t--){ int n; int a[100005]={}; cin>>n; for(int i=0;i<n;i++)cin>>a[i]; sort(a,a+n); double an=0; for(int i=0;i<n-1;i++){ an+=a[i]*(n-1-i); } printf("%.2lf\n",an/n); } }
Python3(3.9) 解法, 执行用时: 507ms, 内存消耗: 10296K, 提交时间: 2021-03-23 14:09:46
for _ in range(int(input())): n=int(input()) m=n an=list(map(int,input().split())) res=0 an.sort() for i in an: res+=i*(n-1) n=n-1 print('%.2f'%(res/m))