一段由凯撒密码加密过的密文,凯撒密码指的是将字符偏移一定的单位,例如若偏移量为2,则a替换为c,b替换为d,c替换为e,...,z替换为b。若加密nowcoder,则密文为pqyeqfgt。现在发现加密包括数字、大写字母、小写字母,即0-9、A-Z、a-z的排列顺序进行偏移,现在截获了对方的一段密文以及偏移量,给定一段密文str和偏移量d,求对应的明文。
"pqyeqfgt",2
"nowcoder"
"123ABCabc",3
"yz0789XYZ"
,d为加密时的偏移量
char* decode(char* str, int d ) { // code == 密码本 const char* code = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; char* ans = (char*) calloc(1001, sizeof(char)); int index; while (*str) { index = strchr(code, *str) - code; strncat(ans, index - d < 0 ? code + (62 + (index - d)) : code + (index - d), 1); ++str; } return ans; }
import java.util.*; public class Solution { /** * 解密密文 * @param str string字符串 密文 * @param d int整型 偏移量 * @return string字符串 */ public String decode (String str, int d) { // write code here HashMap<Character, Integer> map = new HashMap<>(); HashMap<Integer, Character> inverseMap = new HashMap<>(); for(int i = 0; i <= 9; i++){ map.put((char)('0' + i), i); inverseMap.put(i, (char)('0' + i)); } for(int i = 0; i <= 25; i++){ map.put((char)('A' + i), i + 10); inverseMap.put(i + 10, (char)('A' + i)); } for(int i = 0; i <= 25; i++){ map.put((char)('a' + i), i + 36); inverseMap.put(i + 36, (char)('a' + i)); } StringBuilder sb = new StringBuilder(); for(int i = 0; i < str.length(); i++){ char c = str.charAt(i); if(map.get(c) - map.get('0') >= d){ sb.append(inverseMap.get(map.get(c) - d)); }else sb.append(inverseMap.get(map.get(c) - d + 62)); } return sb.toString(); } }
是我想多了,一直在想怎么通过ASCII解决问题
func decode(str string, d int) string { // write code here abracadabra := "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" //97 122 48 57 65 90 abc := []byte(abracadabra) l := len(abc) //log.Info.Println(l) offset := d % 62 //log.Info.Println(offset) b := []byte(str) passwd := []byte{} for i := 0; i < len(b); i++ { //加密文本 for j := 0; j < l; j++ { if b[i] == abc[j] { if j-offset >= 0 { passwd = append(passwd, abc[j-offset]) } else { passwd = append(passwd, abc[j-offset+62]) } } } } ret := string(passwd) return ret }
public String decode (String str, int d) { String order = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; char[]array=str.toCharArray(); int n = 62; int index = 0; for(int i=0; i<str.length(); i++){ char cha=array[i]; if(cha>='0'&&cha<='9') index = cha-'0'; else if(cha>='A' && cha<='Z') index = 10 + (cha - 'A'); else index = 36 + (cha - 'a'); index = (index + n - d) % n; array[i] = order.charAt(index); } return String.valueOf(array); }