description:從給定字串中查詢某指定的字元。
input:輸入的第一行是乙個待查詢的字元。第二行是乙個以回車結束的非空字串(不超過80個字元)。
output:如果找到,在一行內按照格式「index = 下標」輸出該字元在字串中所對應的最大下標(下標從0開始);否則輸出"not found"。
sample input:
mprogramming
sample output:
index = 7
sample input:
a1234
sample output:
not found
c =
input()
str=
input()
i =0
j =-
1n =
false
#判斷字串中是否有指定字元
for ch in
str:
if ch == c and j <= i:
j = i #將最大下標儲存到 j 中
n =true
i +=
1if j >=0:
print
("index ="
, j)
elif
not n:
print
("not found"
)
Python程式設計PTA題解 刪除字元
description 輸入乙個字串 str,再輸入要刪除字元 c,大小寫不區分,將字串 str 現的所有字元 c 刪除。input 在第一行中輸入一行字元 在第二行輸入待刪除的字元 output 輸出僅一行,輸出刪除後的字串 sample input beee sample output resu...
Python程式設計PTA題解 找完數
description 所謂完數就是該數恰好等於除自身外的因子之和。例如 6 1 2 3,其中1 2 3為6的因子。本題要求編寫程式,找出任意兩正整數m和n之間的所有完數。input 輸入僅一行,輸入2個正整數m和n 110000以內的完數如下 1 1 6 1 2 3 28 1 2 4 7 14 4...
Python程式設計PTA題解 詞頻統計
description 編寫程式,對一段英文文字,統計其中所有不同單詞的個數,以及詞頻最大的前10 的單詞。所謂 單詞 是指由不超過80個單詞字元組成的連續字串,但長度超過15的單詞將只擷取保留前15個單詞字元。而合法的 單詞字元 為大小寫字母 數字和下劃線,其它字元均認為是單詞分隔符。input ...