拼多多:第三题
#include<iostream> #include<vector> #include<algorithm> #include<iomanip> using namespace std; bool cmp(int &a, int &b){ return a>b; } /*排列组合*/ int col(int a, int b){ int t=1, ta=1, ba; for (int i = 0; i < b; i++){ t *= a; ta *= (i + 1); a--; } ba = t / ta; //cout <<"ba:"<< ba << endl; return ba; } int main() { int n; cin >> n; vector<int> xi; int t; int sum = 1; int f = 0; for (int i = 0; i < n; i++){ cin >> t; xi.push_back(t); sum *= t; } sort(xi.begin(), xi.end(), cmp); float d = 0.0; int max = xi[0]; int min = 1; for (int i = min; i <= max; i++){ int c = 0; int cs = 0; int pre = 1; for (int j = 0; j<n; j++) { if (xi[j] >= i) c += 1; else pre *= xi[j]; } if (i == 1)cs = 1; else{ int tp = i - 1; for (int p = 1; p <= c; p++){ cs += pow(tp,(c-p))*col(c,p); } } cs *= pre; f += cs * i; } d = float(f) / float(sum); cout << setprecision(3) << d << endl; return 0; }
思路:排列组合