1. 程式人生 > >概率與期望

概率與期望

-a names AI return str sizeof 概率 lightoj end

https://vjudge.net/problem/LightOJ-1030

#include<iostream>
#include<stdio.h>
#include<cstring>
using namespace std;
int main()
{
int t;
cin>>t;
int a[110];
double dp[110];
for(int i=1;i<=t;i++)
{
memset(a,0,sizeof(a));
memset(dp,0,sizeof(dp));
dp[1]=1;
int n,h;
double ans=0;
cin>>n;
for(int j=1;j<=n;j++)
cin>>a[j];
for(int j=1;j<=n;j++){
if(n-j<6)
h=n-j;
else
h=6;
for(int k=1;k<=h;k++)
{
dp[j+k]+=dp[j]*1.0*1/h;
}
ans+=1.0*a[j]*dp[j];
}
printf("Case %d: %lf\n",i,ans);
}
return 0;
}

https://vjudge.net/problem/LightOJ-1027

#include<iostream>
using namespace std;
int gcd(int a,int b)
{
return b>0?gcd(b,a%b):a;
}
int main()
{
int t;
int a[110];
cin>>t;
for(int i=1;i<=t;i++)
{
int n,sum1=0,sum2=0,door1=0,door2=0;
cin>>n;
for(int j=0;j<n;j++)
{
cin>>a[j];
if(a[j]>0)
{
sum1+=a[j];
door1++;
}
else
{
sum2+=-a[j];
door2++;
}
}
int q=gcd(sum1+sum2,n-door2);
cout<<"Case "<<i<<‘:‘<<‘ ‘;
if(door2==n)
cout<<"inf"<<endl;
else
cout<<(sum1+sum2)/q<<‘/‘<<(n-door2)/q<<endl;
}
return 0;
}

概率與期望