给定一个整数,3至18,求3个骰子累加之和为这个整数,打印出这三个骰子各自显示的数

 

// 3RandomSum.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <stdlib.h>
#include "time.h"
using namespace std;

#define myMax(x,y) ( x>y?x:y )
#define myMin(x,y) ( x<y?x:y )

int _tmain(int argc, _TCHAR* argv[])
{
int iSum = 0;
while (scanf_s("%d", &iSum))
{
int iMin = 1;
int s1 = 0;
int s2 = 0;
int s3 = 0;
srand(time(NULL) * 10);
if (iSum <= 18 && iSum >= 3)
{
do
{
int iMax = myMin(6, iSum - 2);
if (iMax > iMin)
{
s1 = iMin + rand() % iMax;
}
else
{
s1 = 1;
}

int iLeft = iSum - 2 - s1;
iMax = myMin(6, iLeft);
if (iMax > iMin)
{
s2 = iMin + rand() % iMax;
}
else
{
s2 = 1;
}
s3 = iSum - s1 - s2;
} while (s1 > 6 || s2 > 6 || s3 > 6);
printf("%d = %d + %d + %d", iSum, s1, s2, s3);
}
else
{
printf("input error,please input 3 to 18");
}

}
return 0;
}

运行结果

3个骰子求和_#include