產生和等於100的5個隨機數
阿新 • • 發佈:2019-01-25
題目:隨機產生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"); }