Kotlin 高中程式練習題 a009: 解碼器
在密碼學裡面有一種很簡單的加密方式,就是把明碼的每個字元加上某一個整數K而得到密碼的字元(明碼及密碼字元一定都在ASCII碼中可列印的範圍內)。例如若K=2,那麼apple經過加密後就變成crrng了。解密則是反過來做。這個問題是給你一個密碼字串,請你依照上述的解密方式輸出明碼。
至於在本任務中K到底是多少,請自行參照Sample Input及Sample Output推出來吧!相當簡單的。
K=7
範例輸入
1JKJ'pz'{ol'{yhklthyr'vm'{ol'Jvu{yvs'Kh{h'Jvywvyh{pvu5 1PIT'pz'h'{yhklthyr'vm'{ol'Pu{lyuh{pvuhs'I|zpulzz'Thjopul'Jvywvyh{pvu5
範例輸出
*CDC is the trademark of the Control Data Corporation. *IBM is a trademark of the International Business Machine Corporation.
var s= "1JKJ'pz'{ol'{yhklthyr'vm'{ol'Jvu{yvs'Kh{h'Jvywvyh{pvu51PIT'pz'h'{yhklthyr'vm'{ol'Pu{lyuh{pvuhs'I|zpulzz'Thjopul'Jvywvyh{pvu5"
var s1= "1JKJ'pz'{ol'{yhklthyr'vm'{ol'Jvu{yvs'Kh{h'Jvywvyh{pvu5"
var s2= "1PIT'pz'h'{yhklthyr'vm'{ol'Pu{lyuh{pvuhs'I|zpulzz'Thjopul'Jvywvyh{pvu5"
var i=0
var c=0
var c1=0
fun main() {
for (i in 0..(s.length-1))
print(s.toCharArray()[i])
println("")
println(s1)
println(s2)
println("")
for (c in s1) {
c1=c.toInt()-7
print( c1.toChar())
}
println("")
for (c in s2) {
c1=c.toInt()-7
print( c1.toChar())
}
}
輸出畫面
1JKJ'pz'{ol'{yhklthyr'vm'{ol'Jvu{yvs'Kh{h'Jvywvyh{pvu51PIT'pz'h'{yhklthyr'vm'{ol'Pu{lyuh{pvuhs'I|zpulzz'Thjopul'Jvywvyh{pvu5 1JKJ'pz'{ol'{yhklthyr'vm'{ol'Jvu{yvs'Kh{h'Jvywvyh{pvu5 1PIT'pz'h'{yhklthyr'vm'{ol'Pu{lyuh{pvuhs'I|zpulzz'Thjopul'Jvywvyh{pvu5 *CDC is the trademark of the Control Data Corporation. *IBM is a trademark of the International Business Machine Corporation.
沒有留言:
張貼留言