当前位置:网站首页>华为机考 ~ 偏移量实现字符串加密
华为机考 ~ 偏移量实现字符串加密
2022-07-26 13:16:00 【weixin_43766298】
1.题目描述:给出原文字符串str,通过对字符串的每个字母进行改变来实现加密,加密方式是在每一个字母str[i]偏移特定数组元素a[i]的量。数组a的前三位已经赋值:a[0]=1,a[1]=2,a[2]=4,当i>=3时,数组元素a[i]=a[i-1]+a[i-2]+a[i-3]。
- 例如:原文abcde加密后bdgkr,其中偏移量分别是1,2,4,7,13。
- 输入描述:第一行是整数n,表示n组测试数据。每组数据包含一行,原文str(只含有小写字母,长度大于0小于50)。
- 输出描述:每组测试数据输出一行,表示密文。
//字符串偏移 xy -> ya , abcde -> bdgkr
public static void tm001(){
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()){
String line1 = scanner.nextLine();
int count = Integer.parseInt(line1);
List<String> stringList = new ArrayList<>();
int maxLen = 0;
for (int i = 0; i < count; i++) {
String input = scanner.nextLine();
if (input.length() > maxLen){
maxLen = input.length();
}
stringList.add(input);
}
// 直接根据最大长度算出 移动步数集合 只计算一次
int[] moveArr = new int[Math.max(maxLen, 3)];
moveArr[0] = 1;
moveArr[1] = 2;
moveArr[2] = 4;
for (int i = 3; i < maxLen; i++) {
//添加的时候 就进行取余 防止叠加超过int范围
//考虑到 Integer.MAX_VALUE = 2147483647,直接对结果进行取余 保证每个数字都不大于26
moveArr[i] = (moveArr[i-1] + moveArr[i-2] + moveArr[i-3]) % 26;// 26个字母
}
for (String line : stringList) {
char[] chars = line.toCharArray();
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < chars.length; i++) {
int move = moveArr[i];
// 'a' = 97 , 'z' = 122
// 防止 出现25 + 25 等情况大于 26的情况出现 再次进行取模
int result = ((chars[i] - 97) + move)%26 + 97; // 26个字母 下标从0开始~25
stringBuilder.append((char)result);
}
System.out.println(stringBuilder);
}
}
}
边栏推荐
- B+ tree selection index (2) -- MySQL from entry to proficiency (23)
- [flower carving hands-on] interesting and fun music visualization series small project (13) -- organic rod column lamp
- AI-理论-知识图谱1-基础
- Sword finger offer (x): rectangular coverage
- MySQL data directory (1) -- database structure (24)
- Use grid to realize left, middle and right layout, and the middle content is adaptive
- MySQL可以自定义变参存储函数吗?
- Can MySQL customize variable parameter storage functions?
- Huawei recruited "talented teenagers" twice this year; 5.4 million twitter account information was leaked, with a selling price of $30000; Google fired engineers who believed in AI consciousness | gee
- Flutter prevents scientific counting and removes mantissa invalid 0
猜你喜欢

postgresql官网下载出错

Niuke brush sql---2
![[upper computer tutorial] Application of integrated stepping motor and Delta PLC (as228t) under CANopen communication](/img/d4/c677de31f73a0e0a4b8b10b91e984a.png)
[upper computer tutorial] Application of integrated stepping motor and Delta PLC (as228t) under CANopen communication

Student examination system based on C #

如何面对科技性失业?
![[5g] what are Cu and Du in 5g?](/img/5b/3453ade88ded4593edfcba9beb8ada.jpg)
[5g] what are Cu and Du in 5g?

Today in history: IBM obtained the first patent; Verizon acquires Yahoo; Amazon releases fire phone

【TypeScript】TypeScript常用类型(上篇)

Can I take your subdomain? Exploring Same-Site Attacks in the Modern Web

12-GuliMall 后台管理中商品系统的品牌管理
随机推荐
Shutter background graying effect, how transparency, gray mask
【花雕动手做】有趣好玩的音乐可视化系列小项目(13)---有机棒立柱灯
A college archives management system based on asp.net
This article explains the FS file module and path module in nodejs in detail
key&key_len&ref&filtered(4)—mysql执行计划(五十)
Guys, how can CDC Oracle set the reading from the specified SCN number, or how to set the read-only full archive, not to read fast
Kubernetes apiserver current limiting strategy
pomerium
B+树索引使用(7)匹配列前缀,匹配值范围(十九)
Leetcode 2119. number reversed twice
JSON format execution plan (6) - MySQL execution plan (52)
LeetCode 69. x 的平方根
Why do you want to make "secret comments"?
HCIP第十一天比较(BGP的配置、发布)
[flower carving hands-on] fun music visualization series small project (12) -- meter tube fast rhythm light
flutter多渠道打包运行
Kubelet CRI 容器运行时
为什么要做“密评”?
B+ tree (5) introduction to MyISAM -- MySQL from getting started to mastering (17)
Kubernetes Flannel:HOST-GW模式