试图扭转我的算法

编程入门 行业动态 更新时间:2024-10-24 01:57:25
本文介绍了试图扭转我的算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我最初创建这个时候花了几天才弄明白

var all =( ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890。# - ?)。split(' '); var 的位置; var oKey = P; function encrypt() // 加密固定 { var sEncode =( HI-MOM)。split(' '); for ( var i = 0 ; i< sEncode.length; i ++){ if (all.indexOf(oKey)< all.indexOf(sEncode) [i])){ position = all.indexOf(sEncode [i]) - all.indexOf(oKey); // output.value + =oKey:+ oKey +距离sEncode [+ i +]:+ sEncode [i] +计数:+位置+最终字符:+所有[位置-1] +\ n; oKey = sEncode [i ]。 } else { position = all.length - all.indexOf(oKey)+ all.indexOf(sEncode [i] ); // output.value + =oKey:+ oKey +距离sEncode [+ i +]:+ sEncode [i] +计数:+位置+最终字符:+所有[位置-1] +\ n; oKey = sEncode [i ]。 } } }

我最初使用break语句来查看后台活动。多数民众赞成在编码中注释掉了() 现在我需要扭转它,这让我起了作用。我一直在思考这个逻辑,它似乎并没有完全相加,因为oKey =P是我偏移的开始并且从那里改变了,但现在我不觉得P会让我失望。

function decrypt() // 抵消固定 { var sEncode = ( 6A4NB#)。split(' '); for ( var i = 0 ; i< sEncode.length; i ++){ if (all.indexOf(oKey)< all.indexOf(sEncode) [i])){ position = all.indexOf(sEncode [i]) - all.indexOf(oKey); output.value + = oKey: + oKey + 到sEncode的距离[ + i + ]: + sEncode [i] + 计数: + position + 最终字符: + all [position - 1 ] + \ n; oKey = sEncode [i]; } else { position = all.length - all.indexOf(oKey)+ all.indexOf(sEncode [i] ); output.value + = oKey: + oKey + 到sEncode的距离[ + i + ]: + sEncode [i] + 计数: + position + 最终字符: + all [position - 1 ] + \ n; oKey = sEncode [i]; } } }

我尝试了什么: 我已尝试过将变量位置换成甚至交换+/-运算符并且只是编码它甚至更多=(

解决方案

首先,我在加密中看到一些问题。

var oKey = P;

oKey 的初始值是从外部设置的加密功能。加密时怎么知道值总是P。 编码后每个在位置中的char,你什么也不做。 该函数什么都不返回。 当你调用解密函数时,你怎么知道 oKey 的价值?

I originally created this whic took me a couple of days to figure out

var all = ("ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890.#-?").split(''); var position; var oKey = "P"; function encrypt() // Encrypt Fixed { var sEncode = ("HI-MOM").split(''); for (var i = 0; i < sEncode.length; i++) { if (all.indexOf(oKey) < all.indexOf(sEncode[i])) { position = all.indexOf(sEncode[i]) - all.indexOf(oKey); //output.value += "oKey: " + oKey + " distance to sEncode[" + i + "]: " + sEncode[i] + " Count: " + position + " Final Char: " + all[position-1] + "\n"; oKey = sEncode[i]; } else { position = all.length - all.indexOf(oKey) + all.indexOf(sEncode[i]); //output.value += "oKey: " + oKey + " distance to sEncode[" + i + "]: " + sEncode[i] + " Count: " + position + " Final Char: " + all[position-1] + "\n"; oKey = sEncode[i]; } } }

I originally used break statements to see the background activity. Thats whats commented out in the encode() Now I need to reverse it which is driving me up the wall. I keep thinking about the logic and it doesnt seem to quite add up, because the oKey="P" was the start of my offset and changed from there, but not now I feel like the "P" is going to throw me off.

function decrypt() // Offset fixed { var sEncode = ("6A4NB#").split(''); for (var i = 0; i < sEncode.length; i++) { if (all.indexOf(oKey) < all.indexOf(sEncode[i])) { position = all.indexOf(sEncode[i]) - all.indexOf(oKey); output.value += "oKey: " + oKey + " distance to sEncode[" + i + "]: " + sEncode[i] + " Count: " + position + " Final Char: " + all[position - 1] + "\n"; oKey = sEncode[i]; } else { position = all.length - all.indexOf(oKey) + all.indexOf(sEncode[i]); output.value += "oKey: " + oKey + " distance to sEncode[" + i + "]: " + sEncode[i] + " Count: " + position + " Final Char: " + all[position - 1] + "\n"; oKey = sEncode[i]; } } }

What I have tried: I've tried every which way swapping variable positions to even swapping the +/- operators and it just encodes it even more =(

解决方案

First of all I see a few problems in encrypt.

var oKey = "P";

The initial value of oKeyis set from outside of the encrypt function. How do know that the value is always "P" when you encrypt. After encoding each char in position, you do nothing with it. The function returns nothing. How do you know the value of oKey when you call the decrypt function.

更多推荐

试图扭转我的算法

本文发布于:2023-11-08 16:05:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1569846.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:算法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!