
107年公務人員高等考試三級考試試題 代號:36340 全四頁
第三頁
類科: 資訊處理
科目: 程式語言
(請接背面)
二、以下是完整的 Python 程式碼,請說明此程式的詳細功能為何?(10 分)
import os.path
file1 = input("Input a file name: ")
file2 = input("Input the other file name: ")
while not os.path.isfile(file1):
print("The file does not exist!")
file1 = input("Input a file name again: ")
while os.path.isfile(file2):
answer = input("The file existed, Overwrite it? y/n? ")
if answer == 'n' or answer=='N':
file2 = input("Input a file name again: ")
else:
break
fileObject1 = open(file1, "r")
fileObject2 = open(file2, "w")
content = fileObject1.read()
fileObject2.write(content)
fileObject1.close()
fileObject2.close()
三、請撰寫一支密碼產生模組程式,此模組的密碼組成為一個五位數字,最後一碼(個
位數)為驗證碼。合法的密碼規則是除了驗證碼之外的所有數字相乘後取除以 10 的
餘數當作驗證碼。例如:13575 即為合法的密碼,因為(1*3*5*7)%10=5。請撰寫一支
完整 C/C++程式,包含 main()與codeGen();codeGen()功能為輸入參數為密碼的前四
碼,輸出為產生驗證碼之後的五位數字密碼。程式讓使用者輸入密碼的前四碼後,
檢查使用者所輸入的數值是否為四位數,正確的話則呼叫 codeGen()將傳回之五位數
字密碼從螢幕顯示出。main()需有迴圈功能詢問使用者是否要繼續輸入,直到使用者
按下不是’Y’或’y’鍵時結束程式。(20 分)
(請接第四頁)