题解 | 凯撒加密
凯撒加密
https://www.nowcoder.com/practice/006b7917d3784371a43cfbae01a9313d
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
in.nextLine();
String s = in.nextLine();
StringBuilder str = new StringBuilder(s);
for(int i = 0;i < s.length(); i++){
char a = str.charAt(i);
str.setCharAt(i,fix(n,a));
}
String result = str.toString();
System.out.print(result);
}
public static char fix(int n, char c){
int c_ascii = (int) c;
if(c_ascii + (n%26) <=122){
return (char)(c_ascii + (n%26));
}else{
return (char)(c_ascii + (n % 26) - 26);
}
}
}
