fork download
  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<ctime>
  4. using namespace std;
  5.  
  6. int main(){
  7. int a[5]={0,0,0,0,0};
  8. int b[5]={0,2,1,2,6};//上限值
  9. int c[5]={0,0,0,0,0};//用來記錄超過限制的格子(先歸零)
  10. int tmp;
  11. int tmp2;
  12. int tmp3;
  13. int i,j;
  14. for(i=0;i<10;i++){
  15. tmp=rand()%5;
  16. a[tmp]++; //現值
  17. }
  18. for(j=0;j<5;j++){
  19. c[j]=(b[j]-a[j])<0?1:0;
  20. }
  21. for(j=0;j<5;j++){
  22. if(c[j])
  23. {
  24. for(i=0;i<(b[j]-a[j]);i++){ //還要注意分配之後 該格子有沒有滿
  25. do{
  26. tmp3=rand()%5;
  27. if(tmp3 != j && (b[tmp3]-a[tmp3])>0)
  28. {
  29. a[tmp3]++;
  30. c[tmp3]=(b[tmp3]-a[tmp3])<0?1:0;
  31. }
  32. }while ((b[j]-a[j]) <0); //若滿了再分給其他格<---這段不知道該怎麼寫
  33. }
  34. }
  35. }
  36. // 第24行~第30行 有疑問~!!!
  37.  
  38. return 0;
  39. }
  40.  
Success #stdin #stdout 0.01s 2720KB
stdin
Standard input is empty
stdout
Standard output is empty