题目:随机产生5个数,这5个数每个数的范围都在[10,35]之间,5个数的和是定值100,尽量让5个数的概率随机,写出算法??
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void main()
{
//int i;
srand((unsigned)time(NULL));
int a,b,c,d,e;
a=(rand()%26)+10;
b=(rand()%26)+10;
if(a!=35&&b!=35)
{
c=(rand()%26)+10;
while((100-a-b-c)/2<10||(100-a-b-c)/2>35)
c=(rand()%26)+10;
d=(rand()%26)+10;
while((100-a-b-c-d)<10||(100-a-b-c-d)>35)
d=(rand()%26)+10;
e=100-a-b-c-d;
}
else
{
c=d=e=10;
}
cout<<a<<endl;
cout<<b<<endl;
cout<<c<<endl;
cout<<d<<endl;
cout<<e<<endl;
system("pause");
}