---
# System prepended metadata

title: 103正式試題

---

# 103正式試題

## pA. 判斷二個數字是否為孿生質數
```python=
for _ in range(int(input())):
    a, b = map(int, input().split(","))
    if abs(a-b) != 2: 
        print("N")
        continue
    for i in range(2, int(a**0.5)+1):
        if not(a%i):
            print("N")
            break
    else:
        for i in range(2, int(b**0.5)+1):
            if not(b%i):
                print("N")
                break
        else:
            print("Y")
```

## pB. 買郵票
```python=
for _ in range(int(input())):
    a, b, c, d = map(int, input().split(","))
    for i in range(a, -1, -1):
        if b*i + c*(a-i) == d:
            print(f"{i},{a-i}")
            break
```

## pC. 在兩字串中，共同出現的英文字母
```python=
for _ in range(int(input())):
    a = set(input())
    b = set(input())
    ans = []
    for i in a:
        if i in b:
            ans.append(i)
    ans.sort()
    print("".join(ans) if ans != [] else "N")
```

## pD. 計算兩個人之間共同朋友的數量
```python=
for _ in range(int(input())):
    a = set([int(i) for i in input().split()][1::])
    b = set([int(i) for i in input().split()][1::])
    ans = 0
    for i in a:
        if i in b:
            ans += 1
    print(ans)
```

## pE. 撲克牌遊戲
```python=
from itertools import combinations

def get_card_value(card):
    """取得撲克牌的點數和花色"""
    card = int(card)
    point = (card - 1) % 13 + 1  # 轉為 1~13 的點數
    suit = (card - 1) // 13      # 轉為 0~3 的花色
    return point, suit

def is_straight(points):
    """判斷是否為順子"""
    points = sorted(set(points))
    for i in range(len(points) - 4):
        if points[i:i+5] == list(range(points[i], points[i] + 5)):
            return True
    # 考慮 A 作為 14 的情況
    if set([1, 10, 11, 12, 13]).issubset(points):
        return True
    return False

def get_hand_score(cards):
    """判斷五張牌的牌型分數"""
    points, suits = zip(*[get_card_value(card) for card in cards])
    point_count = {p: points.count(p) for p in set(points)}
    suit_count = {s: suits.count(s) for s in set(suits)}
    
    # 同花順
    if max(suit_count.values()) == 5 and is_straight(points):
        return 7
    # 四條
    if 4 in point_count.values():
        return 6
    # 葫蘆
    if 3 in point_count.values() and 2 in point_count.values():
        return 5
    # 順子
    if is_straight(points):
        return 4
    # 三條
    if 3 in point_count.values():
        return 3
    # 兩對
    if list(point_count.values()).count(2) == 2:
        return 2
    # 一對
    if 2 in point_count.values():
        return 1
    # 雜牌
    return 0

def get_max_score(cards):
    """取得玩家的最高得分"""
    max_score = 0
    for combo in combinations(cards, 5):
        max_score = max(max_score, get_hand_score(combo))
    return max_score

# 主程式
def main():
    Q = int(input().strip())  # 玩家數量
    results = []
    for _ in range(Q):
        cards = input().split()
        results.append(get_max_score(cards))
    print("\n".join(map(str, results)))

# 測試範例
if __name__ == "__main__":
    main()
```

## pF. 費式數列
```python=
arr = [0, 1]
for i in range(20):
    arr.append(arr[i]+arr[i+1])
arr.sort(reverse=True)
arr.pop();arr.pop()
for _ in range(int(input())):
    ans = ""
    N = n = int(input())
    for i in arr:
        if n-i >= 0:
            ans += "1"
            n -= i
        else:
            ans += "0"
    print(f"{N}={int(ans)}")
```

## pG. 凱薩密碼
```python=
d = {"00":"0", "01":"1", "100":"2", "101":"3", "1100":"4", "1101":"5", "11100":"6", "11101":"7", "111100":"8", "111101":"9"}
key = [0, "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "A", "B", "C"]
for _ in range(int(input())):
    n = input()
    if n[0:2] in d and n[2::] in d:
        print(key[int(d[n[0:2]]+d[n[2::]])])
    else:
        print(key[int(d[n[0:3]]+d[n[3::]])])
```

## pH. 列出所有樹的某節點到根節點之路徑長度
```python=
for _ in range(int(input())):
    m, k, u = map(int, input().split(","))
    tree = []
    for _ in range(m):
        tree.append([int(i) for i in input().split()][1::])
    node = [[0]*(m) for _ in range(k)]
    for i in range(k):
        for j in range(m):
            node[i][j] = tree[j][i]
    a = []
    for i in node:
        root = i.index(999)
        now = i[u]
        ans = 1
        while root != now:
            ans += 1
            now = i[now]
        a.append(ans)
    print(",".join(map(str, a)))
```