有n个人在一个水龙头前排队接水,假如每个人接水的时间为Ti,请编程找出这n个人排队的一种顺序,使得n个人的平均等待时间最小。
输入格式:
共两行,第一行为n(1≤n≤1000);第二行分别表示第1个人到第n个人每人的接水时间T1,T2,…,Tn,每个数据之间有1个空格。
输出格式:
输出为这种排列方案下的平均等待时间(输出结果精确到小数点后两位)。
输入样例:
10 56 12 1 99 1000 234 33 55 99 812
输出样例:
291.90
代码:
#include <iostream> #include <algorithm> #include <iomanip> using namespace std; int main (){ int n; cin>>n; double a[n+1]; for(int i=1;i<=n;i++){ cin>>a[i]; } sort(a+1,a+1+n); double count=0; for(int i=1;i<=n;i++){ for(int j=1;j<i;j++){ count=count+a[j]; } } cout<<fixed<<setprecision(2)<<count/n; }