hdu4405--Aeroplane chess(求期望)
分享一篇求期望的文章 http://kicd.blog.163.com/blog/static/126961911200910168335852/
从后往回推求期望
View Code
1 //Accepted 4405 234MS 8248K 757 B Java 2 import java.util.Scanner; 3 4 public class Main { 5 6 public static void main(String[] args) { 7 8 Scanner sc = new Scanner(System.in); 9 10 while(sc.hasNext()){ 11 12 int n = sc.nextInt(); 13 int m = sc.nextInt(); 14 if(n == 0 && m == 0) break; 15 double e[] = new double[n+1]; 16 int useif[] = new int[n+1]; 17 for(int i = 0; i < m ; ++i){ 18 int a = sc.nextInt(); 19 int b = sc.nextInt(); 20 useif[a] = b; 21 } 22 e[n] = 0; 23 for(int i = n - 1; i >= 0 ; --i){ 24 if(useif[i] > 0){ 25 e[i] = e[useif[i]]; 26 continue; 27 } 28 e[i] = 1; 29 for(int j = 1; j < 7; j++){ 30 if(i + j > n) break; 31 else 32 e[i] += e[i+j]/6; 33 } 34 } 35 System.out.printf("%.4f",e[0]); 36 System.out.println(); 37 } 38 } 39 40 }