2025年2月11日 星期二

柏林邏輯推理

 '''

country,挪威,瑞典,丹麥,德國,英國,

like,狗,鳥,貓,馬,魚

house,藍,紅,綠,白,黃

drink,茶,咖啡,啤酒,牛奶,水

smoke,Prince,Pall_Mall,Dunhill,Blue_Master,Blend

'''

countries = '挪威,瑞典,丹麥,德國,英國'.split(',')

likes = '狗,鳥,貓,馬,魚'.split(',')

houses = '藍,紅,綠,白,黃'.split(',')

drinks = '茶,咖啡,啤酒,牛奶,水'.split(',')

smokes = 'Prince,Pall_Mall,Dunhill,Blue_Master,Blend'.split(',')

c = 1

for country in countries:

  for like in likes:

    for house in houses:

      for drink in drinks:

        for smoke in smokes:

          if country == '英國' and house != '紅':continue

          if country != '英國' and house == '紅':continue

          if country != '瑞典' and like == '狗':continue

          if country == '瑞典' and like != '狗':continue

          print(c,country,like,house,drink,smoke)

          c = c + 1

2025年1月15日 星期三

sorted

 students = [('john', 'A', 20), ('mary', 'B', 30), ('jack','C', 60)]


students = sorted(students,key=lambda x:x[0])

print(students)

students = sorted(students,key=lambda x:x[1])

print(students)

students = sorted(students,key=lambda x:x[2])

print(students)

students = sorted(students,key=lambda x:x[2],reverse=True)

print(students)


# Output:


# [('jack', 'C', 60), ('john', 'A', 20), ('mary', 'B', 30)]

# [('john', 'A', 20), ('mary', 'B', 30), ('jack', 'C', 60)]

# [('john', 'A', 20), ('mary', 'B', 30), ('jack', 'C', 60)]

# [('jack', 'C', 60), ('mary', 'B', 30), ('john', 'A', 20)]


2025年1月14日 星期二

小群體

 n = int(input())

mapping = list(map(int,input().split()))

v = set()

cc = 0


for start in range(n):

  if start not in v:

    c = start

    while c not in v:

      v.add(c)

      c = mapping[c]

    cc += 1

print(cc)


# STDIN

# 5

# 1 2 0 4 3

# Output:

# 2

小群體

# 讀取輸入
n = int(input())
mapping = list(map(int, input().split()))

# 初始化變數
visited = set()  # 使用集合來記錄已訪問的節點
cycle_count = 0  # 記錄閉環數量

# 循環遍歷所有節點
for start_node in range(n):
    if start_node not in visited:  # 如果節點未被訪問
        current_node = start_node
        while current_node not in visited:
            visited.add(current_node)  # 標記當前節點為已訪問
            current_node = mapping[current_node]  # 跳到下一個節點
        cycle_count += 1  # 完成一個閉環

# 輸出結果
print(cycle_count)

改寫後的優化

  1. 可讀性提升:

    • 改善了變數命名,例如:
      • mapping 替代原本的 f。
      • visited 替代原本的 v。
    • 使程式的邏輯更加直觀。
  2. 使用集合:

    • set 結構在檢查和新增訪問節點時比 list 更高效,縮短了運行時間。
  3. 結構簡化:

    • 去除了不必要的變數(如 tg)。
    • 使用 for 迴圈代替了 while 中的手動索引更新,簡化了控制流程。

範例測試

範例 1

輸入:

5
1 2 0 4 3

輸出:

2

範例 2

輸入:

4
1 0 3 2

輸出:

2

範例 3

輸入:

6
1 2 0 4 5 3

輸出:

3

這個改寫版本執行結果與原程式完全一致,但結構更加簡潔,易於理解和維護。

小群體

 # 小群體

n = int(input())

s = [i for i in range(n)]

f = [int(i) for i in input().split()]


v = []


c = 0

ct = 0

while len(v)<len(s):

  tg = [s[c]]

  p = s[c]

  while p not in v:

    v.append(p)

    

    q = f[s.index(p)]

    if q not in v:

      tg.append(q)

    else:

      break

    p = q

  

  ct +=1

  while c<len(s) and s[c] in v:

    c+=1

print(ct)


# STDIN

# 10

# 4 7 2 9 6 0 8 1 5 3

# Output:

# 4

apcs 秘密差_

 

d = input()
s1 = [int(d[i]) for i in range(len(d)) if i%2==1]
s2 = [int(d[i]) for i in range(len(d)) if i%2==0]
print(abs(sum(s1)-sum(s2)))

vr sample

 https://t945935t.blogspot.com/2024/12/vex-vr-sample-ii.html