QBasic 計算還須要淨勝/負多少局才能勝率增/減1%

2014年的12月29日,寫了個VBS腳本,計算還須要連勝多少把才能讓本身的勝率提高1%:編碼

這個腳本的地址在:http://my.oschina.net/Tsybius2014/blog/362078.net

結果後來連着輸了幾把,勝率掉了1%(汗)。正好昨天看了下QBasic,就用QBasic寫段代碼計算一下吧code

個人編碼環境爲:編譯器爲Win32下的FreeBasic(版本1.00.0),IDE爲FBIde(版本0.4.6r4)blog

這段代碼計算了還須要連勝多少把才能把本身的勝率提高1%,也計算了還須要連敗多少把就會讓勝率下降1%ip

計算規則:get

1)以當前玩了426局,獲勝86局爲例,當前的勝率是0.201878,截去小數後是20%。編譯器

2)在這個基礎上,連勝4局後,玩了430局,獲勝90局,勝率爲0.209302,截去小數後仍是20%;但在連勝5局後,玩了431局,獲勝91局,勝率到達0.211137,截去小數後勝率到達21%!這個時候,就認爲是連勝5局後,就能夠讓勝率增長1個百分點。編譯

3)同理,計算負場時,連負4局後,玩了430局,獲勝86局,勝率爲0.2,即20%;連負5局後,玩了431局,獲勝86局,勝率爲0.199536,截去小數後,是19%,視爲勝率降低一個百分點。class

須要注意的幾點是:基礎

1)勝率100%的時候,再連勝多少局勝率都是100%

2)勝率99%的時候,再連勝多少局勝率都是99%

3)勝率0%的時候,再連負多少局勝率都是0%

程序代碼以下:

REM 計算還須要連勝多少局才能讓勝率漲一點
REM 計算還須要連敗多少局才能讓勝率降一點

DIM GAMEPLAYED AS INTEGER 
DIM GAMEWON AS INTEGER  

DIM INPUT1 AS STRING : INPUT "HOW MANY GAMES HAVE YOU PLAYED? "; INPUT1
DIM INPUT2 AS STRING : INPUT "HOW MANY GAMES HAVE YOU WON? "; INPUT2

PRINT

GAMEPLAYED = FIX(VAL(INPUT1))
GAMEWON = FIX(VAL(INPUT2))

PRINT "GAMEPLAYED: "; GAMEPLAYED
PRINT "GAMEWON: "; GAMEWON

PRINT

'輸入合法性校驗
IF GAMEPLAYED < 0 OR GAMEWON < 0 OR GAMEPLAYED < GAMEWON THEN
    PRINT "GAMEPLAYED MUST BIGGER-EQUAL THAN 0"
    PRINT "GAMEWON MUST BIGGER-EQUAL THAN 0"
    PRINT "GAMEPLAYED MUST BIGGER THAN GAMEWON"
    GOTO PRG_FINISH 
END IF

'計算當前勝率
PRINT "CURRENT WINNING RATE: "; GAMEWON * 100 \ GAMEPLAYED; "%"

'計數器
DIM COUNTER AS INTEGER

'計算還須要多少局連勝才能增加一點勝率

'檢查特例
IF GAMEPLAYED = GAMEWON THEN
    PRINT "NO NEED TO IMPROVE!"
    GOTO MISSION1_COMPLETE
ELSEIF GAMEWON * 100 \ GAMEPLAYED = 99 THEN
    PRINT "NO SOLUTION TO IMPROVE!"
    GOTO MISSION1_COMPLETE
END IF

'模擬玩一局贏一局的過程
COUNTER = 1
DO 
    IF (GAMEWON + COUNTER - 1) * 100 \ (GAMEPLAYED + COUNTER - 1) < _
        (GAMEWON + COUNTER) *  100 \ (GAMEPLAYED + COUNTER) THEN
        EXIT DO
    ELSE
        COUNTER = COUNTER + 1
    END IF
LOOP

'打印結論1
PRINT "TO IMPROVE 1% WINNING RATE, YOU NEED TO WIN"; COUNTER; " GAME(S) CONTINUOUSLY"

MISSION1_COMPLETE:

'計算還須要多少局連敗才能下降一點勝率

'檢查特例
IF GAMEWON = 0 THEN
    PRINT "WINNING PERCENTAGE: 0%"
    PRINT "CANNOT DECLINE ANY MORE!"
    GOTO MISSION2_COMPLETE
END IF

'模擬玩一局輸一局的過程
COUNTER = 1
DO 
    IF (GAMEWON) * 100 \ (GAMEPLAYED + COUNTER - 1) > _
        (GAMEWON) *  100 \ (GAMEPLAYED + COUNTER) THEN
        EXIT DO
    ELSE
        COUNTER = COUNTER + 1
    END IF
LOOP

'打印結論2
PRINT "TO DECLINE 1% WINNING RATE, YOU NEED TO LOSE"; COUNTER; " GAME(S) CONTINUOUSLY"

MISSION2_COMPLETE:

PRG_FINISH:
SLEEP
END

運行結果截圖:

END

相關文章
相關標籤/搜索