拼多多:第三题

#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;
}

思路:排列组合


#拼多多##笔试题目##题解#
全部评论

相关推荐

05-29 20:34
门头沟学院 C++
KarlAllen_直通春招版:得做好直接春招的准备。学历差的话,一是面试要求会比学历好的严格不少,二是就算面试通过了也会被排序。总之暑期和秋招对于学历差的就是及其不友好
点赞 评论 收藏
分享
头像
04-17 09:29
已编辑
湖南农业大学 后端
睡姿决定发型丫:本硕末9也是0offer,简历挂了挺多,只有淘天 美团 中兴给了面试机会,淘天二面挂,美团一面kpi面,中兴一面感觉也大概率kpi(虽然国企,但一面0技术纯聊天有点离谱吧)
点赞 评论 收藏
分享
评论
1
7
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务