hdoj 4586 Play the Dice

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4586

解题思路:设期望值为 E, sum=a[1]+a[2]+···+a[n], 由于有 m 个是再来一次机会,故有 E=sum/n+(m/n)*E.

化简得:(n-m)*E=sum. 若 sum=0,则 E=0; 若 n=m, 则 E 为inf; 否则 E=sum/(n-m).

 1 ///////////////////////////////////////////////////////////////////////////
 2 //problem_id: hdoj 4586
 3 //user_id: SCNU20102200088
 4 ///////////////////////////////////////////////////////////////////////////
 5 
 6 #include <algorithm>
 7 #include <iostream>
 8 #include <iterator>
 9 #include <iomanip>
10 #include <cstring>
11 #include <cstdlib>
12 #include <string>
13 #include <vector>
14 #include <cstdio>
15 #include <cctype>
16 #include <cmath>
17 #include <queue>
18 #include <stack>
19 #include <list>
20 #include <set>
21 #include <map>
22 using namespace std;
23 
24 ///////////////////////////////////////////////////////////////////////////
25 typedef long long LL;
26 const double PI=acos(-1.0);
27 
28 const int x4[]={-1,0,1,0};
29 const int y4[]={0,1,0,-1};
30 const int x8[]={-1,-1,0,1,1,1,0,-1};
31 const int y8[]={0,1,1,1,0,-1,-1,-1};
32 
33 typedef int T;
34 T max(T a,T b){ return a>b? a:b; }
35 T min(T a,T b){ return a<b? a:b; }
36 ///////////////////////////////////////////////////////////////////////////
37 
38 ///////////////////////////////////////////////////////////////////////////
39 //Add Code:
40 ///////////////////////////////////////////////////////////////////////////
41 
42 int main(){
43     ///////////////////////////////////////////////////////////////////////
44     //Add Code:
45     int n,m,i,x;
46     while(scanf("%d",&n)!=EOF){
47         int sum=0;
48         for(i=0;i<n;i++){
49             scanf("%d",&x);
50             sum+=x;
51         }
52         scanf("%d",&m);
53         for(i=0;i<m;i++) scanf("%d",&x);
54         if(sum==0) printf("0.00
");
55         else if(n==m) printf("inf
");
56         else printf("%.2lf
",(sum+0.0)/(n-m));
57     }
58     ///////////////////////////////////////////////////////////////////////
59     return 0;
60 }
61 
62 ///////////////////////////////////////////////////////////////////////////
63 /*
64 Testcase:
65 Input:
66 6 1 2 3 4 5 6
67 0
68 4 0 0 0 0
69 1 3
70 Output:
71 3.50
72 0.00
73 */
74 ///////////////////////////////////////////////////////////////////////////