時間限制:C/C++語言 2000MS;其他語言 4000MS
內存限制:C/C++語言 10240KB;其他語言 534528KB
題目描述:
一個字符串,含有字母數字和特殊符號等,按照以下要求寫一個轉換函數
1)只保留小寫字母,過濾掉其他字符,
2)對過濾結果按照下述規則進行映射轉換:
例如若right = 2 :
轉換前->轉換後
a->c
b->d
c->e
…
x->z
y->a
z->b
例如1 輸入字符串:”difg123” right =3, 則最後輸出爲” glij”
例如2 輸入字符串:”yz” 右偏移爲3, 則最後輸出爲” bc”
輸入
輸入佔兩行,包括一個字符串,一個偏移值r
輸出
字符串
樣例輸入
difg123
3
樣例輸出
glij
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner cin = new Scanner(System.in);
String s="";
int r=0;
while(cin.hasNext())
{
s = cin.next();
r = cin.nextInt();
System.out.println(convert(s, r));
}
}
public static String convert(String str, int right){
String res="";
for(int i = 0; i < str.length(); i ++)
{
if(str.charAt(i) >= 'a' && str.charAt(i) <= 'z')
{
if((str.charAt(i) + right) > 'z')
{
//str.charAt(i) - 26:向前移動26個字母
res += (char)(str.charAt(i) - 26 + right);
}else {
res += (char)(str.charAt(i) + right);
}
}
}
return res;
}
}