Question: Use the bipartite method to find out the mathematical expectation.
Policy.
Two mathematical methods are expected: calculate the probability of each number, multiply it with the number, and finally add all the products.
2. mathematical expectation is always equal to the mean, so it can be used as an average. (Simple and Convenient)
Code:
# Include <stdio. h> # include <string. h> int sum; void bi_sear (int n) {sum = 0; int temp, I, left, right, mid; for (I = 1; I <= N; I ++) {temp = 0; left = 1; Right = N; while (1) {++ temp; Mid = (left + right)/2; if (mid = I) {sum + = temp; break;} else if (mid <I) Left = Mid + 1; else right = mid-1 ;}}} int main () {int N; while (scanf ("% d", & n) = 1) {bi_sear (n); printf ("%. 2lf \ n ", (sum * 1.0)/n);} return 0 ;}
Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 3750
Hdoj 3750 guess game [two points of mathematics]