2016-07-23 86 views
-4

簡單的壘球讓某人退出公園:計算勝算比達到百分比的正確編碼公式,以及勝算比的百分比是多少?例如:轉換百分比到賠率和逆轉

Odds to Percent 
1 in 8192 = 0.0001220 

Percent to Odds 
0.0001220 = 1 in 8192 

編程語言是不相關的。

回答

-1
odds_to_percent(a, b): 
    return str(float(a)/b) 

percent_to_odds(x): 
    return '1 in ' + str(1.0/x) 

不確定你到底需要什麼。關於舍入和合作有很多決定。

+0

你肯定賠率百分比不是「返回(雙)a /(b + a)」?修辭問題。順便說一下,幽靈降價會讓我關閉這個問題。 – Krythic

+0

然後定義賠率。我在X *中描述了* 1,而不是* 1與X(部分)*。 – sascha

-1

這就是我想出了:

public struct Odds 
    { 
     private int _a; 
     private int _b; 


     public int A 
     { 
      get 
      { 
       return _a; 
      } 
     } 

     public int B 
     { 
      get 
      { 
       return _b; 
      } 
     } 

     public Odds(int a, int b) 
     { 
      this._a = a; 
      this._b = b; 
     } 

     public Odds(double percent) 
     { 
      Odds odds = FromPercent(percent); 
      this._a = odds.A; 
      this._b = odds.B; 
     } 

     public Odds Invert() 
     { 
      return new Odds(_b, _a); 
     } 

     public double ToPercent() 
     { 
      return ToPercent(_a, _b); 
     } 

     public static double ToPercent(int a, int b) 
     { 
      return ((double)a/(b + a)); 
     } 

     public static Odds FromPercent(double percent) 
     { 
      int multiplier = GetDecimalMultiplier(percent); 
      return new Odds(1, (multiplier - (int)(percent * multiplier))/(int)(percent * multiplier)); 
     } 

     private static int GetDecimalMultiplier(double n) 
     { 
      if (n > 0.01) 
      { 
       return 100; 
      } 
      if (n > 0.001) 
      { 
       return 100000; 
      } 
      if (n > 0.0001) 
      { 
       return 100000000; 
      } 
      throw new Exception("Number too small!"); 
     } 

     public override string ToString() 
     { 
      return A + "-" + B; 
     } 
    } 

賠率爲百分比:

public static double ToPercent(int a, int b) 
    { 
     return ((double)a/(b + a)); 
    } 

百分比來賠率(可能不是完美的)

 public static Odds FromPercent(double percent) 
     { 
      int multiplier = GetDecimalMultiplier(percent); 
      return new Odds(1, (multiplier - (int)(percent * multiplier))/(int)(percent * multiplier)); 
     } 

     private static int GetDecimalMultiplier(double n) 
     { 
      if (n > 0.01) 
      { 
       return 100; 
      } 
      if (n > 0.001) 
      { 
       return 100000; 
      } 
      if (n > 0.0001) 
      { 
       return 100000000; 
      } 
      throw new Exception("Number too small!"); 
     }