2023華為OD統(tǒng)一考試(A+B卷)題庫清單-帶答案(持續(xù)更新)or2023年華為OD真題機(jī)考題庫大全-帶答案(持續(xù)更新)文章來源:http://www.zghlxwxcb.cn/news/detail-643013.html
"給你一串未加密的字符串str,通過對(duì)字符串的每一個(gè)字母進(jìn)行改變來實(shí)現(xiàn)加密,加密方式是在每一個(gè)字母str[i]偏移特定數(shù)組元素a[i]的量,
數(shù)組a前三位已經(jīng)賦值:a[0]=1,a[1]=2,a[2]=4。當(dāng)i=3時(shí),數(shù)組元素a[i]=a[i-1]+a[i-2]+a[i-3],
例如:原文 abcde 加密后 bdgkr,其中偏移量分別是1,2,4,7,13。";
輸入描述:第一行是整數(shù)n,表示n組測試數(shù)據(jù)。每組數(shù)據(jù)包含一行,原文str(只含有小寫字母)
例如:
輸入
1
xy
輸出
ya文章來源地址http://www.zghlxwxcb.cn/news/detail-643013.html
public class StringEncrypt {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int stringLine = Integer.parseInt(sc.nextLine());
String[]encrypts = new String[stringLine];
for (int i = 0; i < stringLine; i++){
encrypts[i] = sc.next();
}
String[] end1 = encryptMe(encrypts);
Arrays.stream(end1).forEach(System.out::println);
}
public static String[] encryptMe(String[]encrypts){
String[] end = new String[encrypts.length];
StringBuffer sb = new StringBuffer();
for (int i = 0; i < encrypts.length; i++){
char[] t1 = encrypts[i].toCharArray();
for (int j = 0; j < t1.length;j++){
//計(jì)算偏移量
int offset1 = offset(j);
//生成字符
char value = (char) ((int)t1[j] + offset1);
if (value > 122){
value = (char) ((int)t1[j] + offset1 - 26);
}
sb.append(value);
}
end[i] = sb.toString();
sb.setLength(0);
}
return end;
}
/**
* 偏移量計(jì)算
* @param i
* @return
*/
public static int offset(int i){
if (i == 0){
return 1;
} else if (i == 1) {
return 2;
} else if (i == 2) {
return 4;
}else {
return offset(i-1) + offset(i-2) + offset(i - 3);
}
}
}
到了這里,關(guān)于華為OD真題--字符串加密的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!