---
# System prepended metadata

title: '#include<iostream>#include<al'

---

# APCS 10月題解
## 1.機械鼠
:::info
題目類型：Ad Hoc
:::
```cpp=
#include<iostream>
using namespace std;
//pl與pr分別用超大以及超小的數字維護位置
int x, n, tmp,  mxl = 0, mxr = 0, pl = 1e9,pr = -1e9;
int main(){
	cin >> x >> n;
	for(int i = 0; i < n; i++){
		cin >> tmp;
		//若食物比老鼠的位置大，則食物在老鼠右邊
		if(x < tmp){
			mxr++;
			pr = max(pr,tmp); //最終老鼠一定會停在最右邊的食物的位置
		}
		//反之則為左邊
		else{
			mxl++;
			pl = min(pl,tmp); //最終老鼠一定會停在最左邊的食物的位置
		}
	}
	//比較左右邊能吃到的食物數量並輸出
	if(mxr > mxl) cout << mxr << " " << pr;
	else cout << mxl << " " << pl;
}
```

## 2.卡牌遊戲
:::info
題目類型：直述式模擬
:::
```cpp=
#include<iostream>
#include<algorithm>
using namespace std;
int n, m, tmp = 0, ans = 0;
int arr[50][50];

void solve()
{
	for(int i = 0; i < n; i++)
	{
		for(int j = 0; j < m; j++)
		{
			if(arr[i][j] == -1)continue;
			
			//向上配對
			tmp = i;
			while(--tmp >= 0 && i - 1 >= 0)
			{
				if(arr[tmp][j] == -1)continue;
				if(arr[tmp][j] == arr[i][j])
				{
					ans += arr[i][j];
					arr[i][j] = -1;
					arr[tmp][j] = -1;
				}
				else break;
			}
			
			//向下配對
			tmp = i;
			while(++tmp < n && i + 1 < n)
			{
				if(arr[tmp][j] == -1)continue;
				if(arr[tmp][j] == arr[i][j])
				{
					ans += arr[i][j];
					arr[i][j] = -1;
					arr[tmp][j] = -1;
				}
				else break;
			}
			//向左配對
			tmp = j;
			while(--tmp >= 0 && j - 1 >= 0)
			{
				if(arr[i][tmp] == -1)continue;
				if(arr[i][tmp] == arr[i][j])
				{
					ans += arr[i][j];
					arr[i][j] = -1;
					arr[i][tmp] = -1;
				}
				else break;
			}
			//向右配對
			tmp = j;
			while(++tmp < m && j + 1 < m)
			{
				if(arr[i][tmp] == -1)continue;
				if(arr[i][tmp] == arr[i][j])
				{
					ans += arr[i][j];
					arr[i][j] = -1;
					arr[i][tmp] = -1;
				}
				else break;
			}
		}
	}
	cout << ans;
}

int main()
{
	cin >> n >> m;
	//輸入表格
	for(int i = 0; i < n; i++)
	{
		for(int j = 0; j < m; j++)
		{
			cin >> arr[i][j];
		}
	}
	solve();
}
```