可以用動態陣列儲存number跟time,
int* num = new int[n];
int* times = new int[n];
然後輸入的部分,判斷方式這樣:
for(int i = 0; i < n; i++){
times[i] -= (times[i]!=0);
if(times[i] == 0){
num[i] = 0;
}
}
int place = -1;
if (num[0] == 0 && num[1] == 0){
place = 0;
}
else{
for(int i = 0; i < n-2;i++){
if (num[i]==0 && num[i+1]==0 &&num[i+2]==0){
place = i+1;
break;
}
}
if (place == -1 && num[n-2] == 0 && num[n-1] == 0){
place = n-1;
}
else if (place == -1){
for(int i = 0; i<n; i++){
if(num[i]==0) {
place = i;
break;
}
}
if (place == -1) cout << " Not enough\n";
}
}
if(place != -1){
num[place] = a;
times[place] = b;
}
place表示要去的小便斗,n是小便斗數量。
當place是預設值-1時表示沒有小便斗了。