秋招模拟赛第36场|2023.07.7-字节实习
- Status
- Done
- Rule
- IOI
- Problem
- 2
- Start at
- 2023-7-26 19:00
- End at
- 2023-7-26 20:00
- Duration
- 1 hour(s)
- Host
- Partic.
- 18
You cannot submit for this problem because the contest is ended. You can click "Open in Problem Set" to view this problem in normal mode.
每个人需要和其他的n−1人作比较,且必须要战胜剩下的n−1人,才可以获胜,因此至多只有1人获胜
如果k>1,则直接输出答案0即可
然后考虑一个人获胜的概率
对于第i个人,如果他想要获胜,则需要按照题目规则去战胜其他的n−1人,按照公式累乘即可
最终把每个人获胜的概率累加,则为一个人获胜的概率
如果k=0,则把上述结果取反即可
O(n2)
C++
#include <bits/stdc++.h>
using namespace std;
int a[505];
double p[505];
int main()
{
int n, k;
cin >> n >> k;
for(int i = 0 ; i < n ; i ++) {
cin >> a[i];
}
double ans = 0;
for(int i = 0 ; i < n ; i ++) {
p[i] = 1;
for(int j = 0 ; j < n ; j ++) {
if(i != j) {
p[i] *= 1.0 * a[i] / (a[i] + a[j]);
}
}
ans += p[i];
}
if(k == 0) ans = 1 - ans;
else if(k != 1) ans = 0;
printf("%.2f\n", ans);
}
python代码
n,k = map(int,input().split()) #参与者人数,获奖者人数
a = list(map(int,input().split()))
prob = [1] * n
for i in range(len(a)): # 第i个人获奖
for j in range(len(a)):
if i != j:
prob[i] *= a[i] / (a[i] + a[j])
if k > 1:
print('0.00')
elif k == 1:
print("%.2f"%sum(prob))
else:
print("%.2f"%(1-sum(prob)))
Java代码
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int k = in.nextInt();
int[] arr = new int[n];
for(int i = 0;i < n;i++) {
arr[i] = in.nextInt();
}
if (k > 1) {
System.out.println("0.00");
return;
}
double ans = 0.00;
for(int i = 0;i < n;i++) {
double t = 1.0;
for(int j = 0;j < n;j++) {
if (i == j)
continue;
t *= (1.0 * arr[i] / (arr[i] + arr[j]));
}
ans += t;
}
ans = k == 1?ans: 1 - ans;
if (ans == 0.0)
System.out.print("0.00");
else
System.out.printf("%.2f", ans);
}
}
有一天,小红在网上冲浪时发现了一个抽奖活动。
活动规则是这样的:假设参与者有 n 个人,每个人通过完成任务或者分享获得奖券,之后任意两人之间都会进行比较获得中奖权。比较方式是这样的,例如有两个人的分别有 x 张奖券和 y 张奖券,那么第一个人获得这一局中奖权的概率是 x+yx,第二个人获得这一局中奖权的概率是 x+yy 。
现在小红想知道恰好有k个人获胜的概率? (一个人中奖需要满足获得 n−1 次中奖权)
输入第一行两个正整数n,k。(3≤n≤110,0≤k≤n)
接下来一行 n 个正整数,第i个数代表 ai 。(1≤ai≤106)
输出一行一个数代表答案,四舍五入保留两位小数。
输入
5 1
1 2 3 4 5
输出
0.44