[kuangbin概率]A Dangerous Maze

tag:通过设概率为未知数解题。
对 期望=概率*结果 更深理解
题意
n扇门,如两扇门每个分别为 +x +x,则可用 x+x 的时间出去,如为 −x−x,则可用 x+x 的时间回到原点。每次随机选一个,问出去的期望时间。
题解

由式子可以看出,当n负==n的时候,永远无法离开迷宫。

#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
   
 return b ? gcd(b,a%b) : a;
}
int main()
{
   
 int t,cnt=0;
 scanf("%d", &t);
 while(t--) {
   
  int n,sum=0,ans1=0;
  scanf("%d", &n);
  for(int i=1; i<=n; i++) {
   
   int t;
   scanf("%d", &t);
   if(t<0) {
   
    ans1++;
    t=-t;
   }
   sum+=t;
  }
  if(ans1==n) {
   
   printf("Case %d: inf\n",++cnt);
  }
  else {
   
   int s=gcd(sum,(n-ans1)); //要约分 
   printf("Case %d: %d/%d\n",++cnt,sum/s,(n-ans1)/s);
  }
 }
 return 0;
}
全部评论

相关推荐

好羡慕走AI技术方向的人,实习薪资这么高😱&nbsp;人比人气死人😭
勇敢的嘟教授在考古:芯片公司挣得多多的,钱给的还不如互联网
点赞 评论 收藏
分享
点赞 评论 收藏
分享
AI牛可乐:哇,听起来你很激动呢!杭州灵枢维度科技听起来很厉害呀~你逃课去白马培训,老冯会同意吗?不过既然你这么感兴趣,肯定是有原因的吧! 对了,想了解更多关于这家公司或者求职相关的问题吗?可以点击我的头像私信我哦,我可以帮你更详细地分析一下!
你都用vibe codi...
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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