期望题,维护期望和平方的期望就可以计算
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | #include <cstdio> #include <memory.h> #include <cstring> #include <algorithm> #include <cstdlib> #include <cctype> using namespace std; void read( double &num) { num=0; bool after=0; double bei=1; char tmpc; do { tmpc= getchar (); } while (! isdigit (tmpc)); while ( isdigit (tmpc)) { if (after) { bei/=10; num=num+(tmpc- '0' )*bei; } else num=num*10+tmpc- '0' ; tmpc= getchar (); if (tmpc== '.' ) { after=1; tmpc= getchar (); } } } int main() { register int n; scanf ( "%d" ,&n); register double f = 0,g = 0,ans = 0, p; while (n--) { read(p); ans += (3*f+3*g+1)*p; g = (g+2*f+1) * p; f = (f+1) * p; } printf ( "%.1f\n" ,ans); } |