骰子求和 c++
阿新 • • 發佈:2018-12-10
#include<iostream> #include<iomanip> #define MAXLENGTH 100 using namespace std; void dict(int* result, int num, int sum) { if (num == 0) { //int b = result[sum]; result[sum] += 1; } else { for (int i = 1; i <= 6; i++) { sum += i; dict(result, --num, sum); sum -= i; ++num; } } } int main() { int n; int result[MAXLENGTH]; memset(result, 0, sizeof(result)); cin >> n; cin.clear(); cin.sync(); dict(result, n, 0); for (int i = n; i <= n * 6; i++) { cout <<'['<< i<<']'<< setprecision(2) << (1.0*result[i])/ pow(6.0, n) << endl; } }