3個骰子求和

2021-07-08 12:15:06 字數 638 閱讀 7712

給定乙個整數,3至18,求3個骰子累加之和為這個整數,列印出這三個骰子各自顯示的數

// 3randomsum.cpp : 定義控制台應用程式的入口點。

//#include "stdafx.h"

#include #include "time.h"

using namespace std;

#define mymax(x,y) ( x>y?x:y )

#define mymin(x,y) ( x= 3)

else

int ileft = isum - 2 - s1;

imax = mymin(6, ileft);

if (imax > imin)

else

s3 = isum - s1 - s2;

} while (s1 > 6 || s2 > 6 || s3 > 6);

printf("%d = %d + %d + %d", isum, s1, s2, s3);

} else

} return 0;

}

執行結果

Lintcode 20 骰子求和

扔 n個骰子,向上面的數字之和為 s。給定 given n,請列出所有可能的 s值及其相應的概率。樣例給定n 1,返回 1,0.17 2,0.17 3,0.17 4,0.17 5,0.17 6,0.17 思路 扔n個骰子數字和及概率相當於求前n 1個骰子和及概率與第n個骰子和及概率。1.當n 0,返...

lintcode 18 骰子求和

扔 n 個骰子,向上面的數字之和為 s。給定 n,請列出所有可能的 s值及其相應的概率。樣例 1 輸入 n 1 輸出 1,0.17 2,0.17 3,0.17 4,0.17 5,0.17 6,0.17 解釋 擲一次骰子,向上的數字和可能為1,2,3,4,5,6,出現的概率均為 0.17。樣例 2 輸...

lintcode 20 骰子求和

扔 n 個骰子,向上面的數字之和為 s。給定 given n,請列出所有可能的 s 值及其相應的概率。注意事項 you do not care about the accuracy of the result,we will help you to output results.樣例給定 n 1,返...