2021.4.11 字节跳动实习笔试题—情报解密
题目内容
题目内容:
给你一个字符串,该字符串中有数字,字母(字母只有大写字母)以及一些其他的字符(例如:%,.等),如果首字母为字母或者数字的话,就将整个字符串中的字母和字符都移动首字母对应的ASCll值的,就比如"A123"就解密为"B234",而如果首字母的为"%",则就将"%"看做是"A"即可,如果是其他的字符,则不需要解密。
import java.util.Scanner;
public class qingbao {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String str = in.nextLine();
char[] res = str.toCharArray();
int temp;
if('A' <= res[0] && res[0] <= 'Z'){
temp = res[0] - 'A' + 1;
}
else if('0' <= res[0] && '9' >= res[0]){
temp = res[0] - '0';
}else{
temp = 1;
}
for(int i = 0; i < res.length; i++){
if('A' <= res[i] && res[i] <= 'Z'){
res[i] = (char)((res[i] + temp - 'A') % 26 + 'A');
}
else if('0' <= res[i] && res[i] <= '9'){
res[i] = (char)((res[i] + temp - '0') % 10 + '0');
}
else{
continue;
}
}
System.out.println(new String(res));
}
}