2025年5月15日 星期四

#計算a/b,無條件捨去到小數點以下第25位

 #計算a/b,無條件捨去到小數點以下第25位


from decimal import Decimal

while True:

try:

a,b = map(int,input().split())

r = Decimal(a)/Decimal(b)

rr = f'{r:.26f}'

print(rr[:-1])

except:

break

2025年5月13日 星期二

text.encode

 #_*_coding=big5_*_


while True:

try:

text = input()

hex_output = ''.join([f'{byte:02x}' for byte in text.encode('big5')])

hex_output = hex_output.upper()

print(hex_output)

except:

break

2025年5月12日 星期一

打字練習程式:分段學習結構

 

單元 目標 重點學習
第1單元 建立 GUI 視窗與顯示句子 tkinter 基本介面、文字顯示
第2單元 建立輸入框並接收輸入 Entry 輸入框與事件綁定
第3單元 計算正確率與耗時 time.time() 與比對邏輯
第4單元 多題練習與換題功能 random.choice()、變數更新
第5單元 完整統計、重新開始功能 題數統計、平均計算、重啟流程
`

打字練習程式

 import tkinter as tk

import random

import time


# 建立句子庫

sentences = [

    "The quick brown fox jumps over the lazy dog.",

    "Python is a powerful programming language.",

    "Practice makes perfect.",

    "Typing is a useful skill.",

    "Artificial intelligence is changing the world."

]


# 全域變數

target_sentence = random.choice(sentences)

start_time = 0

question_count = 0

total_time = 0

total_accuracy = 0

results = []  # 儲存每題耗時與正確率


def start_typing():

    global start_time

    entry.delete(0, tk.END)

    entry.focus()

    start_time = time.time()

    result_label.config(text="")


def check_typing(event=None):

    global start_time, question_count, total_time, total_accuracy, results


    end_time = time.time()

    user_input = entry.get()

    elapsed_time = round(end_time - start_time, 2)


    correct = sum(1 for a, b in zip(target_sentence, user_input) if a == b)

    accuracy = round(correct / len(target_sentence) * 100, 2)


    # 統計資料儲存

    question_count += 1

    total_time += elapsed_time

    total_accuracy += accuracy

    results.append((question_count, elapsed_time, accuracy))


    if question_count == 5:

        show_summary()

    else:

        next_question()


def next_question():

    global target_sentence

    target_sentence = random.choice(sentences)  # ✅ 真正換句子

    sentence_label.config(text=target_sentence)

    entry.delete(0, tk.END)

    entry.focus()


def show_summary():

    avg_accuracy = round(total_accuracy / 5, 2)

    result_text = "📊 練習結束!以下是你的統計資料:\n\n"

    for num, t, acc in results:

        result_text += f"第 {num} 題:耗時 {t} 秒,正確率 {acc}%\n"

    result_text += f"\n✅ 總耗時:{round(total_time, 2)} 秒"

    result_text += f"\n✅ 平均正確率:{avg_accuracy}%"


    result_label.config(text=result_text)

    entry.config(state='disabled')  # 鎖住輸入框

    sentence_label.config(text="練習結束,請重新啟動程式再來一次")


# 建立主視窗

window = tk.Tk()

window.title("打字練習程式")

window.geometry("800x500")


# 「開始」按鈕放右上

start_button = tk.Button(window, text="開始", command=start_typing)

start_button.place(x=730, y=10)


# 題目區

tk.Label(window, text="請輸入以下句子:", font=("Arial", 16)).pack(pady=20)

sentence_label = tk.Label(window, text=target_sentence, font=("Arial", 12), fg="blue", wraplength=750)

sentence_label.pack()


# 輸入框

entry = tk.Entry(window, font=("Arial", 12), width=80)

entry.place(x=20, y=320)

entry.bind("<Return>", check_typing)


# 顯示結果與統計

result_label = tk.Label(window, text="", font=("Arial", 14), justify="left")

result_label.pack(pady=10)


# 啟動主程式

window.mainloop()

2025年5月9日 星期五

insert sort python edition

 d = list(range(110,100,-1))+list(range(1,11))

print(d)

t = d[0:]


for i in t:

  d = [j for j in d if j<i] + [i] + [j for j in d if j>i]

print(d)


Output:


[110, 109, 108, 107, 106, 105, 104, 103, 102, 101, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110]

2025年5月8日 星期四

求質數 II

 primes = []  # 建立一個空列表,用來儲存質數


for p in range(2, 101):  # 檢查從 2 到 100 的每一個數字

    is_prime = True  # 假設目前的數字 p 是質數


    for i in range(2, int(p**0.5) + 1):  # 從 2 檢查到 √p

        if p % i == 0:  # 如果 p 被整除,表示不是質數

            is_prime = False

            break  # 提前跳出迴圈,沒必要再檢查下去


    if is_prime:

        primes.append(p)  # 如果是質數,加入列表


print(primes)  # 印出所有找到的質數


求質數

print( [ p for p in range(2,100+1) if all(p%i!=0 for i in range(2,int(p**0.5)+1)) ])


Output:

[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]