golang 轮训加密算法
Roy's friends has been spying on his text messages, so Roy thought of an algorithm to encrypt text messages.
Encryption Algorithm is as follows:
We say message to be encrypted as Plain Text and encrypted form of message as Cipher.
Plain Text consists of lower case alphabets only.
Consider the Cipher Disk as shown in figure.

Initially, we start with 0 (zero). For each character in Plain Text, we move either clockwise or anti-clockwise on the disk depending on which way is closest from where we are currently standing.
If both clockwise and anti-clockwise distances are equal, we give priority to clockwise movement.
Clockwise movements are represented using positive numbers while Anti-clockwise movements are represented as negative numbers.
Roy needs your help in implementing this algorithm. Given a Plain Text message, your task is to encrypt it using above algorithm and print the Cipher Text.
Input:
First line contains integer T - number of test cases.
Each of next T lines contains a string representing Plain Text message.
Output:
For each test case, print the encrypted form of given string in new line.
Each line should consist of space separated integers in the range [-12,13].
See the sample test case for more clarification.
Constraints:
1 <= T <= 100
1 <= Length of Plain Text string <= 100
Sample Test Case Explanation:
Explanation for 3rd sample test case "correct"

3
aeiou
hackerearth
correct
0 4 4 6 6
7 -7 2 8 -6 13 13 -4 -9 2 -12
2 12 3 0 13 -2 -9
We begin from 0 (zero) 1. 'a'->'c' - two steps clockwise, we reach 'c' 2. 'c'->'o' - twelve steps clockwise, we reach 'o' 3. 'o'->'r' - three steps clockwise, we reach 'r' 4. 'r'->'r' - we are already at 'r', so zero steps 5. 'r'->'e' - thirteen steps clockwise, we reach 'e' 6. 'e'->'c' - here moving anti-clockwise is optimal, so two steps anticlockwise, and for anticlockwise we add negative sign. 7. 'c'->'t' - again anti-clockwise, nine steps.
就是用0到26来代码A到Z字母,然后根据输入的字符串转换成,两个字母间的最小距离,用到了判断绝对值的函数。其中 leftValue 为顺时针计算的距离,rightValue 是逆时针计算的距离,然后取绝对值小的数值。
package main import (
"fmt"
) var step int = 97 func CalcAbs(a int) (ret int) {
ret = (a ^ a>>31) - a>>31
return
}
func main() {
var numCount int
fmt.Scanln(&numCount)
var question string
var Previous int = -1
for i := 0; i < numCount; i++ {
fmt.Scanln(&question)
for _, v := range question {
if Previous == -1 {
Previous = int(v) - 97
fmt.Print(int(v)-97, " ")
} else {
var current = int(v) - 97
var leftValue, rightValue int
if current >= Previous {
rightValue = current - Previous
leftValue = 26 - current + Previous
} else {
leftValue = 26 - Previous + current
rightValue = current - Previous
}
//fmt.Printf("left:%d-right:%d", left, right)
//fmt.Println("")
if CalcAbs(leftValue) > CalcAbs(rightValue) {
fmt.Print(rightValue, " ")
} else {
fmt.Print(leftValue, " ")
}
Previous = current
}
}
fmt.Println("")
}
}
golang 轮训加密算法的更多相关文章
- RSA非对称加密算法实现:Golang
RSA是1977年由罗纳德·李维斯特(Ron Rivest).阿迪·萨莫尔(Adi Shamir)和伦纳德·阿德曼(Leonard Adleman)一起提出的.当时他们三人都在麻省理工学院工作.RSA ...
- AES对称加密算法实现:Java,C#,Golang,Python
高级加密标准(Advanced Encryption Standard,简写AES),是一种用来替代DES的对称加密算法,相比DES,AES安全性更高,加密速度更快,因此被广泛使用. 理论上看,AES ...
- DES对称加密算法实现:Java,C#,Golang,Python
数据加密标准(DES,Data Encryption Standard,简写DES)是一种采用块密码加密的对称密钥算法,加密分组长度为64位,其中8.16.24.32.40.48.56.64 等8位是 ...
- 不同程序语言处理加密算法的性能对比(PHP/Golang/NodeJS)
首先上我最常用且最易用的PHP来做下测试 <?php function test_encrypt($fun, $max) { $begin = microtime(TRUE); $pwdpre ...
- [Golang]Go Packages
---------------------------------------------------------------------------------------------------- ...
- <转>golang 并发性能数据
1.管道chan吞吐极限10,000,000,单次Put,Get耗时大约100ns/op,无论是采用单Go程,还是多Go程并发(并发数:100, 10000, 100000),耗时均没有变化,Go内核 ...
- Golang 探索对Goroutine的控制方法
前言 在golang中,只需要在函数调用前加上关键字go即可创建一个并发任务单元,而这个新建的任务会被放入队列中,等待调度器安排.相比系统的MB级别线程栈,goroutine的自定义栈只有2KB,这使 ...
- Golang的CSP很酷?其实.NET也可以轻松完成
说起Golang(后面统称为Go),就想到他的高并发特性,在深入一些就是 Goroutine.在大家被它优雅的语法和简洁的代码实现的高并发程序所折服时,其实C#/.NET也可以很容易的做到.今天我们来 ...
- golang数据传输格式-序列化与反序列化
golang数据传输格式-序列化与反序列化 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 想必计算机专业毕业的小伙伴应该都知道数据想要持久化存储,必须将其存在I/O设备里面,这些I ...
随机推荐
- linux进程管理和系统状态查看命令简介
1 进程管理简介 进程(Process)是计算机中的程序关于某数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位,是操作系统结构的基础 2 常用命令 2.1 pstree 2.1.1 功能描 ...
- C++ 函数对象
函数对象 c++中函数名后的()称为函数调用运算符.函数调用运算符也可以重载,如果某个类重载了函数调用运算符,则该类的实例就是一个函数对象.函数对象本身并不是很有用,但他们使得算法操作的参数化策略成为 ...
- UILabel 的使用
直接上代码: /* UILabel 使用 */ UILabel *aLable = [[UILabel alloc] initWithFrame:self.window.bounds]; aLable ...
- 《Deep Learning》译文 第一章 前言(中) 神经网络的变迁与称谓的更迭
转载请注明出处. 第一章 前言(中) 1.1 本书适合哪些人阅读? 能够说本书的受众目标比較广泛,可是本书可能更适合于例如以下的两类人群.一类是学习过与机器学习相关课程的大学生们(本科生或者研究生). ...
- Java的单例模式
单例模式:单例模式确保其一个类只有一个实例,而且自行实例化并向整个系统提供这个实例. 单例模式又分为:懒汉式,饿汉式等; 特点: a.单例只有一个实例. b.必须自己创建自己唯一的实例 c.单例类必须 ...
- Learning to Rank算法介绍:GBRank
之前的博客:http://www.cnblogs.com/bentuwuying/p/6681943.html中简单介绍了Learning to Rank的基本原理,也讲到了Learning to R ...
- python 素因子分解
在使用python解决问题之前,我们先说一下,什么是素因子分解 所谓素因子分解就是,先找这个数的所有约数(约数即:a%b == 0,也就是a可以被b整除) 例如:20的约数集合为 [1, 2, 5, ...
- FastDFS并发会有bug,其实我也不太信?- 一次并发问题的排查经历
前一段时间,业务部门同事反馈在一次生产服务器升级之后,POS消费上传小票业务偶现异常,上传小票业务有重试机制,有些重试三次也不会成功,他们排查了一下没有找到原因,希望架构部帮忙解决. 公司使用的是Fa ...
- Android开发之常见事件响应方式
常见的事件有 (1)单击事件 onClickListener (2)长按事件 onLongClickListener (3)滑动事件 onTouchListener (4)键盘事件 onKeyLi ...
- 【MySQL】数据库字段类型
1.数值型 整型 TINYINT SMALLINT MEDIUMINT INT BIGINT 浮点型 FLOAT(m,n) - m表示总位数,n表示小数位数. DOUBLE(m,n) DECIMAL( ...