C++参考代码:
#include<iostream>
#include<map>
#include<vector>
using namespace std;
int main()
{vector<char> x;vector<char> y;map<char,char> word;char ch = getchar();getchar();string str;getline(cin,str);for(char c=ch;c<='Z';c++){y.push_back(c);}for(char c='A';c<ch;c++){y.push_back(c);}for(char c = 'A';c<='Z';c++){x.push_back(c);}for(int i=0;i<26;i++){word.insert(pair<char,char>(x[i],y[i]));}for(int i=0;i<str.length();i++){if(str[i]>='A'&&str[i]<='Z'){cout<<word[str[i]];}else{cout<<str[i];}}return 0;
}
Java参考代码:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner scan = new Scanner(System.in);Map<Character, Character> map = new HashMap<Character, Character>();ArrayList<Character> word = new ArrayList<>();String key = scan.nextLine();String essay = scan.nextLine();for(char ch = key.charAt(0);ch<='Z';ch++) word.add(ch);for(char ch = 'A';ch<key.charAt(0);ch++) word.add(ch);char words='A';for(char c:word) {map.put(words, c);words++;}for(int i=0;i<essay.length();i++) {if(essay.charAt(i)>='A'&&essay.charAt(i)<='Z') System.out.print(map.get(essay.charAt(i)));else System.out.print(essay.charAt(i));}scan.close();}
}