---
# System prepended metadata

title: ZeroJudge 解答

---

## 歷屆試題題號整理

| 題號 | 名稱 | 日期 |
| - | - | - |
| [o076](https://zerojudge.tw/ShowProblem?problemid=o076) | 特技表演 | 113/06 |
| [o077](https://zerojudge.tw/ShowProblem?problemid=o077) | 電子畫布 | 113/06 |
| [o078](https://zerojudge.tw/ShowProblem?problemid=o078) | 缺字問題 | 113/06 |
| [o079](https://zerojudge.tw/ShowProblem?problemid=o079) | 最佳選擇 | 113/06 |
| [m931](https://zerojudge.tw/ShowProblem?problemid=m931) | 遊戲選角 | 113/01 |
| [m932](https://zerojudge.tw/ShowProblem?problemid=m932) | 蜜蜂觀察 | 113/01 |
| [m933](https://zerojudge.tw/ShowProblem?problemid=m933) | 邏輯電路 | 113/01 |
| [m934](https://zerojudge.tw/ShowProblem?problemid=m934) | 合併成本 | 113/01 |
| [m370](https://zerojudge.tw/ShowProblem?problemid=m370) | 機械鼠 | 112/10 |
| [m371](https://zerojudge.tw/ShowProblem?problemid=m371) | 卡牌遊戲 | 112/10 |
| [m372](https://zerojudge.tw/ShowProblem?problemid=m372) | 搬家 | 112/10 |
| [m373](https://zerojudge.tw/ShowProblem?problemid=m373) | 投資遊戲 | 112/10 |
| [k731](https://zerojudge.tw/ShowProblem?problemid=k731) | 路徑偵測 | 112/06 |
| [k732](https://zerojudge.tw/ShowProblem?problemid=k732) | 特殊位置 | 112/06 |
| [k733](https://zerojudge.tw/ShowProblem?problemid=k733) | 磁軌移動序列 | 112/06 |
| [k734](https://zerojudge.tw/ShowProblem?problemid=k734) | 開啟寶盒 | 112/06 |
| [j605](https://zerojudge.tw/ShowProblem?problemid=j605) | 程式考試 | 112/01 |
| [j606](https://zerojudge.tw/ShowProblem?problemid=j606) | 造字程式 | 112/01 |
| [j607](https://zerojudge.tw/ShowProblem?problemid=j607) | 先加後乘與函數 | 112/01 |
| [j608](https://zerojudge.tw/ShowProblem?problemid=j608) | 機器出租 | 112/01 |
| [i428](https://zerojudge.tw/ShowProblem?problemid=i428) | 巴士站牌 | 111/10 |
| [j123](https://zerojudge.tw/ShowProblem?problemid=j123) | 運貨站 | 111/10 |
| [j124](https://zerojudge.tw/ShowProblem?problemid=j124) | 石窟探險 | 111/10 |
| [j125](https://zerojudge.tw/ShowProblem?problemid=j125) | 蓋步道 | 111/10 |
| [i399](https://zerojudge.tw/ShowProblem?problemid=i399) | 數字遊戲 | 111/06 |
| [i400](https://zerojudge.tw/ShowProblem?problemid=i400) | 字串解碼 | 111/06 |
| [i401](https://zerojudge.tw/ShowProblem?problemid=i401) | 雷射測試 | 111/06 |
| [i402](https://zerojudge.tw/ShowProblem?problemid=i402) | 內積 | 111/06 |
| [h081](https://zerojudge.tw/ShowProblem?problemid=h081) | 程式交易 | 111/01 |
| [h082](https://zerojudge.tw/ShowProblem?problemid=h082) | 贏家預測 | 111/01 |
| [h083](https://zerojudge.tw/ShowProblem?problemid=h083) | 數位占卜 | 111/01 |
| [h084](https://zerojudge.tw/ShowProblem?problemid=h084) | 牆上海報 | 111/01 |
| [g595](https://zerojudge.tw/ShowProblem?problemid=g595) | 修補圍籬 | 110/11 |
| [g596](https://zerojudge.tw/ShowProblem?problemid=g596) | 動線安排 | 110/11 |
| [g597](https://zerojudge.tw/ShowProblem?problemid=g597) | 生產線 | 110/11 |
| [g598](https://zerojudge.tw/ShowProblem?problemid=g598) | 真假子圖 | 110/11 |
| [g275](https://zerojudge.tw/ShowProblem?problemid=g275) | 七言對聯 | 110/09 |
| [g276](https://zerojudge.tw/ShowProblem?problemid=g276) | 魔王迷宮 | 110/09 |
| [g277](https://zerojudge.tw/ShowProblem?problemid=g277) | 幸運數字 | 110/09 |
| [g278](https://zerojudge.tw/ShowProblem?problemid=g278) | 美食博覽會 | 110/09 |
| [f605](https://zerojudge.tw/ShowProblem?problemid=f605) | 購買力 | 110/01 |
| [f606](https://zerojudge.tw/ShowProblem?problemid=f606) | 流量 | 110/01 |
| [f607](https://zerojudge.tw/ShowProblem?problemid=f607) | 切割費用 | 110/01 |
| [f608](https://zerojudge.tw/ShowProblem?problemid=f608) | 飛黃騰達 | 110/01 |
| [f312](https://zerojudge.tw/ShowProblem?problemid=f312) | 人力分配 | 109/10 |
| [f313](https://zerojudge.tw/ShowProblem?problemid=f313) | 人口遷移 | 109/10 |
| [f314](https://zerojudge.tw/ShowProblem?problemid=f314) | 勇者修練 | 109/10 |
| [f315](https://zerojudge.tw/ShowProblem?problemid=f315) | 低地距離 | 109/10 |
| [f579](https://zerojudge.tw/ShowProblem?problemid=f579) | 購物車 | 109/07 |
| [f580](https://zerojudge.tw/ShowProblem?problemid=f580) | 骰子 | 109/07 |
| [f581](https://zerojudge.tw/ShowProblem?problemid=f581) | 圓環出口 | 109/07 |
| [f582](https://zerojudge.tw/ShowProblem?problemid=f582) | 病毒演化 | 109/07 |
| [h026](https://zerojudge.tw/ShowProblem?problemid=h026) | 猜拳模擬 | 109/01 |
| [h027](https://zerojudge.tw/ShowProblem?problemid=h027) | 矩陣總和 | 109/01 |
| [h028](https://zerojudge.tw/ShowProblem?problemid=h028) | 砍樹 | 109/01 |
| [h029](https://zerojudge.tw/ShowProblem?problemid=h029) | 自動分裝 | 109/01 |
| [b964](https://zerojudge.tw/ShowProblem?problemid=b964) | 成績指標 | 105/03 |
| [b965](https://zerojudge.tw/ShowProblem?problemid=b965) | 矩陣轉換 | 105/03 |
| [b966](https://zerojudge.tw/ShowProblem?problemid=b966) | 線段覆蓋長度 | 105/03 |
| [b967](https://zerojudge.tw/ShowProblem?problemid=b967) | 血緣關係 | 105/03 |
| [c294](https://zerojudge.tw/ShowProblem?problemid=c294) | 三角形辨識 | 105/10 |
| [c295](https://zerojudge.tw/ShowProblem?problemid=c295) | 最大和 | 105/10 |
| [c296](https://zerojudge.tw/ShowProblem?problemid=c296) | 定時K彈 | 105/10 |
| [c297](https://zerojudge.tw/ShowProblem?problemid=c297) | 棒球遊戲 | 105/10 |
| [c290](https://zerojudge.tw/ShowProblem?problemid=c290) | 秘密差 | 106/03 |
| [c291](https://zerojudge.tw/ShowProblem?problemid=c291) | 小群體 | 106/03 |
| [c292](https://zerojudge.tw/ShowProblem?problemid=c292) | 數字龍捲風 | 106/03 |
| [c575](https://zerojudge.tw/ShowProblem?problemid=c575) | 基地台 | 106/03 |
| [c461](https://zerojudge.tw/ShowProblem?problemid=c461) | 邏輯運算子 | 106/10 |
| [c462](https://zerojudge.tw/ShowProblem?problemid=c462) | 交錯字串 | 106/10 |
| [c463](https://zerojudge.tw/ShowProblem?problemid=c463) | 樹狀圖分析 | 106/10 |
| [c471](https://zerojudge.tw/ShowProblem?problemid=c471) | 物品堆疊 | 106/10 |
| [e286](https://zerojudge.tw/ShowProblem?problemid=e286) | 籃球比賽 | 108/06 |
| [e287](https://zerojudge.tw/ShowProblem?problemid=e287) | 機器人的路徑 | 108/06 |
| [e288](https://zerojudge.tw/ShowProblem?problemid=e288) | 互補CP | 108/06 |
| [e289](https://zerojudge.tw/ShowProblem?problemid=e289) | 美麗的彩帶 | 108/06 |


## a001
```python
print('hello, ' + input())
```

## a002
```python
a = input().split()
print(int(a[0]) + int(a[1]))
```

## a003
```python
m, d = map(int, input().split())
s = (m * 2 + d ) % 3
print(['普通', '吉', '大吉'][s])
```

## a004
```python
while True:
    try:
        y = int(input())
        if (( y % 4 == 0) and ( y %100 != 0)) or (y % 400 == 0):
            print('閏年')
        else:
            print('平年')
    except:
        break 
```

## a005
提示：如何輕鬆取出前 4 個數字並轉化成整數
```python!
a, b, c, d = map(int,input().split())
```

解答：
```python!
n = int(input())

for i in range(n):
  a, b, c, d = map(int,input().split())
  if b - a == c - b:
    print(a, b, c, d, int(d + b - a))
  else:
    print(a, b, c, d, int(d * (b / a)))
```

## a009
提示：如何在字元與編碼之間相互轉換
```python!
ord('A') #將字元轉化成編碼 'A' -> 65
chr(65) #將編碼轉化成字元 65 -> 'A'
```

```python!
code = input()
text = ''
for c in code:
    text += chr(ord(c) - 7)
print(text)
```

## c636
提示：注意使用者可能輸入負數的民國年

```python!
import sys

a = '鼠牛虎兔龍蛇馬羊猴雞狗豬'
b = a[::-1]

for c in sys.stdin:
    i = int(c)
    if i > 0: print(a[(i - 1) % 12])
    if i < 0: print(b[(abs(i) - 1) % 12])
```

## a020
提示：使用 table 字典去儲存字母與數字的對照表

```python
table = {
  'A':10,'B':11,'C':12,'D':13,'E':14,'F':15,'G':16,'H':17,'I':34,
  'J':18,'K':19,'L':20,'M':21,'N':22,'O':35,'P':23,'Q':24,'R':25,
  'S':26,'T':27,'U':28,'V':29,'W':32,'X':30,'Y':31,'Z':33
}

id = input() #取得身分證字號


t = table[id[0]]
a = t // 10
b = t % 10

answer = a + b * 9
answer += int(id[1])*8 + int(id[2])*7 + int(id[3])*6 + int(id[4])*5
answer += int(id[5])*4 + int(id[6])*3 + int(id[7])*2 + int(id[8])*1 + int(id[9])*1

if answer % 10 == 0: print('real')
else: print('fake')
```

## a024

提示：使用 while 迴圈進行輾轉相除法，當其中一個數值被另一個整除時，最小公因數就出來了

```python!
[a, b] = input().split()
a = int(a)
b = int(b)

while a != 0 and b != 0:
  a = a % b
  a, b = b, a

print(a)
```

## d097

```python!
import sys

for line in sys.stdin:
    try:
        arr = list(map(int, line.split()))
        
        a = []
        b = [i for i in range(1, arr[0])]
    
        for i in range(1, len(arr) - 1):
          diff = abs(arr[i] - arr[i + 1])
          a.append(diff)

        a.sort()

        if a == b:
          print('Jolly')
        else:
          print('Not jolly')
        
    except:
      break

```

## a524

```python!
import sys

def test(arr, s):
    if arr == '': print(s)
    for c in arr:
      test(arr.replace(c, ''), s + c)

for line in sys.stdin:
    try:
        x = int(line)
        arr = [str(i) for i in range(x, 0, -1)]
        test(''.join(arr))
    except:
        break
```

## a054

```python
code = {'A':10, 'B':11, 'C':12, 'D':13, 'E':14, 'F':15, 'G':16, 'H':17, 'I':34,
         'J':18, 'K':19, 'L':20, 'M':21, 'N':22, 'O':35, 'P':23, 'Q':24, 'R':25,
         'S':26, 'T':27, 'U':28, 'V':29, 'W':32, 'X':30, 'Y':31, 'Z':33}

for k in code:
  code[k] = (code[k] // 10) + (code[k] % 10) * 9

x = [int(i) for i in input()]

s = x[0] * 8 + x[1] * 7 + x[2] * 6 + x[3] * 5 + x[4] * 4 + x[5] * 3 + x[6] * 2 + x[7] * 1

for c in code:
    if 10 - (code[c] + s) % 10 == x[8]:
      print(c, end='')
```

## a065

```python
a = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
code = {}

for i in range(26): code[a[i]] = i

p = [code[c] for c in input()]

#步驟：計算出之間的數值差並印出

```

## c657

## d478

## a417

提示：

```python
n = int(input())

for i in range(n):

  N, D = map(int, input().split())

  grid = [[0 for i in range(N)] for i in range(N)]

  if N % 2 == 1: grid[N // 2][N // 2] = N*N

  count = 1
  for n in range(N // 2):
      x = n
      y = n
      for i in range(4):
          for j in range(N - n * 2 - 1):
              grid[y][x] = count
              if i == 0: x += 1
              elif i == 1: y += 1
              elif i == 2: x -= 1
              elif i == 3: y -= 1
              count += 1

  for y in range(N):
    for x in range(N):
      if D == 1:
        print(str(grid[y][x]).rjust(5), end='')
      else:
        print(str(grid[x][y]).rjust(5), end='')
    print()
```

# a468

```python!
import re
from math import ceil, floor

table = {
    "January": 1, "February": 2, "March": 3, "April": 4, "May": 5, "June": 6,
    "July": 7, "August": 8, "September": 9, "October": 10, "November": 11, "December": 12
}

n = int(input())

for x in range(n):
    
    m1, d1, y1 = input().split()
    m2, d2, y2 = input().split()
    
    y1 = int(y1)
    y2 = int(y2)
    d1 = int(d1[:-1])
    d2 = int(d2[:-1])
    m1 = table[m1]
    m2 = table[m2]
    
    if m1 > 2: y1 += 1
    if m2 == 1 or (m2 == 2 and d2 < 29): y2 -= 1
    
    count = 0
    
    s = ceil(y1 / 4) * 4
    e = floor(y2 / 4) * 4
    if e >= s: count += (e - s) / 4 + 1
    
    s = ceil(y1 / 100) * 100
    e = floor(y2 / 100) * 100
    if e >= s: count -= (e - s) / 100 + 1
    
    s = ceil(y1 / 400) * 400
    e = floor(y2 / 400) * 400
    if e >= s: count += (e - s) / 400 + 1
    
    print(f'Case {x + 1}: {int(count)}')
```

# o076
```python!
m = int(input())
arr = list(map(int, input().split()))

count = 0
best = 0

for i in range(1, m):
    if arr[i - 1] > arr[i]:
        count += 1
        best = max(best, count)
    else:
        count = 0

print(best + 1)
```

# o077

```python!
h, w, n = map(int, input().split())

grid = [[0 for x in range(w)] for y in range(h)]

for i in range(n):
    r, c, t, x = map(int, input().split())
    
    for yy in range(max(r - t, 0), min(r + t + 1, h)):
        for xx in range(max(c - t, 0), min(c + t + 1, w)):
            if abs(xx - c) + abs(yy - r) <= t:
                grid[yy][xx] += x

for row in grid:
    print(*row)
```

# o078

```python!
import itertools

a = input()
n = int(input())
s = input()

arr = [s[i:i+n] for i in range(len(s) - n + 1)]
arr = set(arr)
arr = sorted(arr)

idx = 0

for ss in itertools.product(a, repeat=n):
    x = ''.join(ss)
    if idx < len(arr) and x == arr[idx]:
        idx += 1
    else:
        print(x)
        break
```

# o079

```python
from bisect import bisect_left

n, k = map(int, input().split())
v = list(map(int, input().split()))

mp = { 0: [(0, 0)] }

total = 0
diff = 0

for i in range(1, n + 1):
    total += v[i - 1]
    diff += -1 if v[i - 1] % 2 else 1

    if total > k: continue

    if diff not in mp:
      mp[diff] = []
    mp[diff].append((i, total))

if 0 in mp:
  ans = mp[0][-1][1]
else:
  ans = 0

total = 0
diff = 0

for i in range(n - 1, -1, -1):
    total += v[i]
    diff += -1 if v[i] % 2 else 1

    if -diff not in mp: continue

    endpts = mp[-diff]

    l = 0
    r = bisect_left(endpts, (i + 1, -1))

    while l < r:
        mid = (l + r) // 2
        if endpts[mid][1] + total > k:
            r = mid
        else:
            l = mid + 1

    if l > 0:
        ans = max(ans, endpts[l - 1][1] + total)

print(ans)
```


```python
grid = [
    [8, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 3, 6, 0, 0, 0, 0, 0],
    [0, 7, 0, 0, 9, 0, 2, 0, 0],
    [0, 5, 0, 0, 0, 7, 0, 0, 0],
    [0, 0, 0, 0, 4, 5, 7, 0, 0],
    [0, 0, 0, 1, 0, 0, 0, 3, 0],
    [0, 0, 1, 0, 0, 0, 0, 6, 8],
    [0, 0, 8, 5, 0, 0, 0, 1, 0],
    [0, 9, 0, 0, 0, 0, 4, 0, 0],
]

def get_valid(x, y):
    if grid[y][x] != 0: return []

    possible_numbers = set(range(1, 10))

    for num in grid[y]:
        possible_numbers.discard(num)

    for row in grid:
        possible_numbers.discard(row[x])

    start_row = (y // 3) * 3
    start_col = (x // 3) * 3
    for i in range(3):
        for j in range(3):
            possible_numbers.discard(grid[start_row + i][start_col + j])

    return list(possible_numbers)

# 遞迴填入答案
def solve():
    pass

solve()
for row in grid: print(row)
```


# m370

```python
a, b = map(int, input().split())
arr = list(map(int, input().split()))

r_idx = a
r_count = 0
l_idx = a
l_count = 0

for x in arr:
    if x > a:
        r_count += 1
        if x > r_idx: r_idx = x
    else:
        l_count += 1
        if x < l_idx: l_idx = x

if r_count > l_count:
    print(r_count, r_idx)
else:
    print(l_count, l_idx)
```

# m371

```python
Y, X = map(int, input().split())

grid = []
for _ in range(Y):
    row = list(map(int, input().split()))
    grid.append(row)


total = 0

while True:
    
    diff = False #是否異動
    
    # 檢查水平線
    for y in range(Y):
        a = 0
        b = 1
        while b < X:
            if grid[y][a] == grid[y][b] and grid[y][a] != '*':
                total += grid[y][a]
                grid[y][a] = '*'
                grid[y][b] = '*'
                a = b + 1
                b = a + 1
                diff = True
            elif grid[y][b] == '*':
                b += 1
            else:
                a = b
                b += 1
    
    # 檢查垂直線
    ...(自行完成)
            
    if not diff: break
        
print(total)

```

# m372

```python!
H, W = map(int, input().split())

grid = []
grid.append(['0' for i in range(W + 2)])
for i in range(H):
    grid.append(['0'] + [s for s in input()] + ['0'])
grid.append(['0' for i in range(W + 2)])

def search(y, x):
    arr = [(y, x)]
    count = 0
    while len(arr) > 0:
        y, x = arr.pop()
        if grid[y][x] == '0': continue
        tmp = grid[y][x]
        grid[y][x] = '0'
        count += 1
        if tmp in "XLHF" and grid[y][x + 1] in "XH7J": arr.append((y, x + 1))
        if tmp in "XH7J" and grid[y][x - 1] in "XLHF": arr.append((y, x - 1))
        if tmp in "XIF7" and grid[y + 1][x] in "XILJ": arr.append((y + 1, x))
        if tmp in "XILJ" and grid[y - 1][x] in "XIF7": arr.append((y - 1, x))
    return count

ans = 0
for y in range(H):
    for x in range(W):
        if grid[y][x] != '0':
            ans = max(ans, search(y, x))

print(ans)
```

# m932

```python

```