---
tags: YTP/APCS 練習題單
---
# c086: M * A * S * H
題目link:https://zerojudge.tw/ShowProblem?problemid=c086
這題是一個不用管時間複雜度的模擬題
N * X只有20*55 大,就算用O(n^2^)的方法也會過
比較複雜的是資料輸入,只寫while(cin >> n >> x)將導致程式不知道怎麼結束
透過 !cin.eof()作為循環條件,讓程式直到EOF才不進行迴圈
不然它會傻傻的一直等n和x,傳上去也是CE和SE
```cpp=
#include<bits/stdc++.h>
using namespace std;
const int N = 55;
int n, x, ai[25], t = 0;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
while(cin >> n >> x && !cin.eof())
{
t++;
bool a[N] = {0};
for(int i = 0; i < 20; i ++) cin >> ai[i];
int survive = n, idx = 0;
while(x < survive){
int cnt = 0;
for(int i = 1;i <= n;i ++){
if (a[i] == 0) cnt ++;
if (cnt == ai[idx]){
survive -= 1;
a[i] = true;
cnt = 0;
if (survive == x) break;
}
}
idx++;
if (idx == 20) break;
}
cout << "Selection #" << t <<endl;
for(int i = 1; i <= n; i ++)
{
if (!a[i]) cout << i << " ";
}
cout <<endl<<endl;
}
}
```