龙空技术网

HackerRank凯撒密码(1 Week Preparation Kit: Caesar Cipher)

马尚突 7

前言:

眼前姐妹们对“用c语言实现凯撒密码”可能比较关注,同学们都想要分析一些“用c语言实现凯撒密码”的相关知识。那么小编在网上汇集了一些对于“用c语言实现凯撒密码””的相关知识,希望朋友们能喜欢,姐妹们一起来了解一下吧!

Julius Caesar protected his confidential information by encrypting it using a cipher. Caesar's cipher shifts each letter by a number of letters. If the shift takes you past the end of the alphabet, just rotate back to the front of the alphabet. In the case of a rotation by 3, w, x, y and z would map to z, a, b and c.

Julius Caesar 通过使用密码对其进行加密来保护他的机密信息。凯撒的密码将每个字母移动了多个字母。如果换档带您超过字母表的末尾,只需旋转回字母表的前面。在旋转 3 的情况下,w、x、y 和 z 将映射到 z、a、b 和 c。

alphabet: abcdefghijklmnopqrstuvwxyz

Alphabet rotated +3: defghijklmnopqrstuvwxyzabc

原始字母:abcdefghijklmnopqrstuvwxyz字母旋转+3:defghijklmnopqrstuvwxyzabc

例子(Example)

s= There's-a-starman-waiting-in-the-sky

The alphabet is rotated by 3 , matching the mapping above. The encrypted string is Wkhuh'v-d-vwdupdq-zdlwlqj-lq-wkh-vnb..

Note: The cipher only encrypts letters; symbols, such as -, remain unencrypted.

字母被旋转3,匹配上面的映射。加密的字符串是Wkhuh'v-d-vwdupdq-zdlwlqj-lq-wkh-vnb.

注意:密码加密字母;符号,例如-,保持未加密。

功能说明(Function Description)

Complete the caesarCipher function in the editor below.

caesarCipher has the following parameter(s):

string s: cleartext

int k: the alphabet rotation factor

在下面的编辑器中完成caesarCipher函数。

caesarCipher 具有以下参数:

字符串 s:明文int k : 字母旋转因子

返回(Returns)

string: the encrypted string

string: 加密后的字符串

输入格式(Input Format)

The first line contains the integer, n, the length of the unencrypted string.

The second line contains the unencrypted string,s .

The third line contains k , the number of letters to rotate the alphabet by.

第一行包含整数,n,未加密字符串的长度。

第二行包含未加密的字符串,s.

第三行包含k, 字母旋转字母的数量。

约束(Constraints)

1<=n<=100

0<=k<=100

s is a valid ASCII string without any spaces.

s是没有任何空格的有效 ASCII 字符串。

样本输入(Sample Input)

11middle-Outz2

样本输出(Sample Output)

okffng-Qwvb

解释(Explanation)

Original alphabet:      abcdefghijklmnopqrstuvwxyzAlphabet rotated +2:    cdefghijklmnopqrstuvwxyzab原始字母:abcdefghijklmnopqrstuvwxyz字母旋转+2:cdefghijklmnopqrstuvwxyzabm -> oi -> kd -> fd -> fl -> ne -> g-    -O -> Qu -> wt -> vz -> b

(请在留言区留下您的解决方案编程语言不限)

Language Python 3

#!/bin/python3def caesarCipher(s, k):    # Write your code here    m = 'abcdefghijklmnopqrstuvwxyz'    w = m[k%26:]+m[:k%26]    result = ''    for i in s:        if i in m:            m_i = m.find(i)             result+=result.join(w[m_i])        elif i in m.upper():            m_i = m.upper().find(i)             result+=result.join(w.upper()[m_i])        else :            result+=result.join(i)    return result

标签: #用c语言实现凯撒密码