Little Book Boy-Caesar's Code
Subject link: https://www.luogu.com.cn/problem/P1914
Topic background
A certain Konjac became obsessed with the "little book boy", and one day he forgot his password when logging in (he did not bind his email or cell phone), so he threw the question to Shen Ben you.
Title description
Although Konjac has forgotten his password, he still remembers that the password is composed of a character string. The password is formed by moving back each letter in the original string (consisting of no more than 50 lowercase letters) by nn bits. The next letter of z is a, and so on. He has now found the original text string and nn before the move, please find the password.
Input format
The first line: n. The second line: a string of letters before moving
Output format
One line is the password of this Konjac
Sample input and output
Enter #1
1
qwe
Output #1
rxf
Problem-solving ideas:
((c[i]-'a' + n)% 26 +'a') Perform shift letter conversion, pay attention to type conversion.
code show as below:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String s = sc.next();
char[] c = s.toCharArray();
for (int i = 0; i < s.length(); i++) {
c[i] = (char) ((c[i] - 'a' + n) % 26 + 'a');
System.out.print(c[i]);
}
System.out.println();
}
}