# APCS實作題練習(詳解)
###### tags: `APCS`
## 【題解】ZeroJudge h081: 1. 程式交易
https://zerojudge.tw/ShowProblem?problemid=h081

```python=
#python
n,d=map(int,input().split())
m=list(map(int,input().split()))
x=m[0] # 現在持有股票的金額
ans=y=0 # y = 賣掉後,的最後金額
for i in range(1,len(m)):
if(x!=-1): # 要是有股票
if(m[i]>=x+d): # 且現在金額 >=原本持有 +d
ans+=m[i]-x # 賺錢
x=-1 # 設為賣掉
y=m[i] # 賣掉後,的最後金額
else : #沒有股票
if(m[i]<=y-d): # 且現在金額 <=原本持有-d
x=m[i] #設定,現在持有的股票金額
print(ans)
```
## 【題解】ZeroJudge h082: 2. 贏家預測
https://zerojudge.tw/ShowProblem?problemid=h082


```python=
n,m=map(int,input().split())
k=[0]+[int(x) for x in input().split()] # 戰力
p=[0]+[int(x) for x in input().split()] # 應變力
alive=[int(x) for x in input().split()] # 編號
n_lose=[0]*(n+1)
while(len(alive)>1): #存活大於1個,繼續打
winner =[]
loser=[] #不能到最後,不然沒人打
for i in range(0,len(alive)-1,2): # 兩個兩個打
f,s=alive[i],alive[i+1] # 2 個對打 alive照編號排的,不會有0號,所以前面k=[0]+[int(x) for x in input().split()]要補0
a,b,c,d=k[f],p[f],k[s],p[s] # a (1)戰力 b(1)應變力 c (2)戰力 d(2)應變力
if(a*b>=c*d): #對打
win=f
k[f]+=c*d//(2*b)
p[f]+=c*d//(2*a)
lose=s
k[s]+=c//2
p[s]+=d//2
else :
win=s
k[s]+=a*b//(2*d)
p[s]+=a*b//(2*c)
lose=f
k[f]+=a//2
p[f]+=b//2
winner.append(win) #贏的編號
n_lose[lose]+=1 #輸的,敗場 +1
if(n_lose[lose]<m): #<m 才可以存活
loser.append(lose)
if(len(alive)%2==1) : #要是有烙單
winner.append(alive[-1]) # 放在存活的最後一個 ([-1]就是隊伍的最後一個)
alive=winner +loser #贏前 輸後
print(alive[0])
```
## 【題解】ZeroJudge g595: 1. 修補圍籬
https://zerojudge.tw/ShowProblem?problemid=g595


```python=
n=int(input())
m=[int(x) for x in input().split()]
ans=0
for i in range(0,len(m)):
if( i==0 and (m[i]==0)): #要是是在最左邊,且圍籬斷掉
ans+=m[i+1] #直接加右邊的值
continue #強制跳出 ❮本次❯ 迴圈,繼續進入下一圈
if( i==len(m)-1 and (m[i]==0)): #要是是在最右邊,且圍籬斷掉
ans+=m[i-1] #直接加左邊的值
continue #強制跳出 ❮本次❯ 迴圈,繼續進入下一圈
if(m[i]==0): #有斷掉
ans+=min(m[i-1],m[i+1]) #找左右小的,並加上去
print(ans)
```
## 【題解】ZeroJudge g596: 2. 動線安排
### @@待修改


```python=
# 1水平 2垂直 1+2=3十字 0 空白 4柱子
def rem(r,c): #拔柱子
global m,n
a[r][c]=0
i=c-1 #往左邊找
while(i >=0 and (a[r][i]==1 or a[r][i]==3)): #當沒有出界,且是線
a[r][i]-=1 #清除 水平線 -1 直接清空 十字線 -1 變成只有水平線
i-=1 #繼續往左
i=c+1 #往右邊找
while(i <n and (a[r][i]==1 or a[r][i]==3)) : #當沒有出界,且是線
a[r][i]-=1 #清除 水平線 -1 直接清空 十字線 -1 變成只有水平線
i+=1 #繼續往右
i=r-1 #往下邊找
while(i >=0 and (a[i][c]==2 or a[i][c]==3)) : #當沒有出界,且是線
a[i][c]-=2
i-=1
i=r+1 #往上邊找
while(i <m and (a[i][c]==2 or a[i][c]==3)) : #當沒有出界,且是線
a[i][c]-=2
i+=1
def add(r,c):
global m,n
if (a[r][c]!=2 and a[r][c]!=3):
r2=r-1
while(r2 >=0 and a[r2][c]!=4) : r2-=1
if(r2>=0) :
for i in range(r-1,r2,-1):
a[i][c]+=2
r2=r+1
while(r2 <m and a[r2][c]!=4) : r2+=1
if(r2<m) :
for i in range(r+1,m,1):
a[i][c]+=2
if(a[r][c]!=1 and a[r][c]!=3) :
c2=c-1 #當沒有出界,且不是柱子
while(c2 >=0 and a[r][c2]!=4) : c2-=1 #繼續往左
if(c2>=0): #找到柱子
for i in range(c-1,c2,-1): a[r][i]+=1 #拉線
c2=c+1
while(c2 <n and a[r][c2]!=4) : c2+=1 #繼續往右
if(c2<n):
for i in range(c+1,c2,1): a[r][i]+=1 #拉線
a[r][c]=4
total=0 #每回合非空格數量
mymax=0 #最大值
m,n,h=map(int,input().split())
a=[[0]*n for j in range(m)] #二維陣列出話
for i in range(h):
r,c,indel=map(int,input().split())
if indel==0:
add(r,c)
else :
rem(r,c)
total=0
for i in range(m):
total+=n-a[i].count(0)
if total>mymax: mymax=total
print(mymax)
print(total)
```
## 【題解】ZeroJudge g275: 1. 七言對聯

```python=
n=int(input())
for i in range(n):
a = list(map(int,input().split()))
b = list(map(int,input().split()))
aa=bb=cc=True
if(a[1]!=a[3] and a[1]==a[5] and b[1]!=b[3] and b[1]==b[5]):
aa=True
else :
aa=False
print('A',end='')
if(a[6]==1 and b[6]==0):
bb=True
else :
bb=False
print('B',end='')
if(a[1]!=b[1] and a[3]!=b[3] and a[5]!=b[5]):
cc=True
else :
cc=False
print('C',end='')
if(aa==True and bb==True and cc==True):
print('None',end='')
print()
```
## 【題解】ZeroJudge g276: 2. 魔王迷宮
修改

```python=
n=int(input())
for i in range(n):
s=input()
ans=1
for j in range(0,len(s)):
ans*=int(s[j])
print(ans)
```
## 【題解】f605: 1. 購買力

```c=
//c++
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int n,d;
cin>>n>>d;
int total=0;
int num=0;
for (int i=0;i<n;i++)
{
int arr[3];
cin>>arr[0]>>arr[1]>>arr[2];
sort(arr,arr+3);
if(arr[2]-arr[0]>=d)
{
int a=0;
a=((arr[0]+arr[1]+arr[2])/3);
total+=a;
num++;
}
}
cout<<num<<" "<<total;
}
```
:::