转换百分比与比值比和反转

时间:2016-07-23 16:44:10

标签: formula probability

轻松的垒球可以让某人击出公园:将比值比计算为百分比的正确编码公式是什么,以及百分比与比值比?例如:

Odds to Percent
1 in 8192 = 0.0001220

Percent to Odds
0.0001220 = 1 in 8192

编程语言无关紧要。

2 个答案:

答案 0 :(得分:-1)

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

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

不确定你到底需要什么。关于舍入和合作有很多决定。

答案 1 :(得分:-1)

这是我想出的:

#include <ncurses.h>
#include <string.h> 
#include <string>
#include <unistd.h>
#include <iostream>

using namespace std;

class ColorWindow {
    private:
        bool canColor;
        WINDOW* container;
        int height, width, startx, starty;

    public:
        ColorWindow() : canColor(false), container(nullptr) {
            if(has_colors()) {
                canColor=true;
            }

            this->height = 20;
            this->width = 84;
            this->starty = (LINES - height) / 2;    /* Calculating for a center placement */
            this->startx = (COLS - width) / 2;
        }

        bool writeStringWithColor(int x, int y, const char* message) {
            if(!canColor) {
                writeString(3, 5, "Sorry, your term can't show colors.");
                return false;
            }

            init_pair(1, COLOR_RED, COLOR_BLACK);

            writeString(0, 10, "aaaaaaaaa");

            wattron(getContainer(), COLOR_PAIR(1));
            writeString(x, y, message);
            wattroff(getContainer(), COLOR_PAIR(1));
            return true;
        }

        void writeString(int x, int y, const char* message) {
            mvwprintw(getContainer(), y, x, message);
        }

        WINDOW* createNewContainer() {
            this->container = newwin(height, width, starty, startx);
            wrefresh(this->container);      /* Show that box        */

            return getContainer();
        }

        WINDOW* getContainer() {
            return this->container;
        }

        void refreshContainer() {
            refresh();
            wrefresh(this->container);      /* Show that box        */
        }       
};

int main() {
    initscr();          /* Start curses mode        */
    cbreak();           /* Line buffering disabled, Pass on
                        * everything to me      */
    keypad(stdscr, TRUE);
    start_color();

    ColorWindow cw = ColorWindow();

    cw.createNewContainer();

    bool success = cw.writeStringWithColor(0, 10, "Hello everyone in color!!");
    if(!success)
        cw.writeString(0, 10, "Write with color failed :(");
    cw.refreshContainer();
    sleep(2);
    endwin();

    return 0;
}

百分比赔率:

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));
    }