当前位置:网站首页>华为机考 ~ 偏移量实现字符串加密
华为机考 ~ 偏移量实现字符串加密
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);
}
}
}
边栏推荐
- Use float to realize left, middle and right layout, and the middle content is adaptive
- Shutter background graying effect, how transparency, gray mask
- MySQL data directory (1) -- database structure (24)
- [typescript] typescript common types (Part 2)
- LeetCode 217. 存在重复元素
- Sword finger offer (x): rectangular coverage
- 基于C#实现的学生考试系统
- Implementation of SAP ABAP daemon
- Outline design specification
- [5gc] what is 5g slice? How does 5g slice work?
猜你喜欢

C regards type as generic type T and uses it as generic type of method

基于C#实现的学生考试系统
![[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

Example of establishing socket communication with Siemens PLC based on C # open TCP communication

Slam 02. overall framework

flutter多渠道打包运行

jvm:类加载子系统干什么的?由什么组成?需要记住哪些八股文?

Kubelet CRI 容器运行时

File upload and download performance test based on the locust framework

One stroke problem (Chinese postman problem)
随机推荐
MySQL data directory (3) -- table data structure MyISAM (XXVI)
Can I take your subdomain? Exploring Same-Site Attacks in the Modern Web
B+树索引使用(9)分组、回表、覆盖索引(二十一)
基于C#开放式TCP通信建立与西门子PLC的socket通信示例
AI theory knowledge map 1 Foundation
基于C#实现的学生考试系统
How to face scientific and technological unemployment?
LeetCode 2119. 反转两次的数字
B+树(3)聚簇索引,二级索引 --mysql从入门到精通(十五)
概要设计说明书
银行业客户体验管理现状与优化策略分析
子组件触发父组件自定义事件(defineEmits):子组件传值给父组件
基于BERT的情感分析模型
key&key_ Len & ref & filtered (4) - MySQL execution plan (50)
[applet] why can't the onreachbottom event be triggered? (one second)
Reflection, an implementation of automatic repeated call interface
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
父组件访问子组件的方法或参数 (子组件暴漏出方法defineExpose)
【TypeScript】TypeScript常用类型(下篇)
关于自动重复调用接口的一种实现方式-反射