第一次试水…只会vb(6.0)的某不知名高中生
第一篇就讲讲之前遇到的一个很有意思的题好了:
已知字符的ascii码可以用8位二进制数来表示,某同学想用以下加密方法进行加密:将原八位二进制码每四位进行前后交换
将得到的二进制码再转化为字符输出 即:0000 1111→1111 0000
解题思想:拿到这个题下手方向有两个 一是用mid函数强行进行交换 二是转化用16进制的数来考虑
分析一下第一个思路:虽然是可行的但是写出代码会比较冗长(对原ascii值进行取余倒数为二进制再用mid函数)理解上会更简单 但原题是个填空题需要以一行代码实现交换 显然不太可行
第二个:16进制数如18→81 会比较容易实现 而一行代码中实现交换并转为10进制输出也比较容易实现
注意点:对于字符要进行单个取出处理(想起来当时演示的时候有人问为什么不直接全部先除得到对应的值 没能及时解答这里补上 )全除后要对首末位处理比较困难orz
当时写的时候想过会不会有>9的情况 后来发现不用担心 进行╲及mod运算得到的都是<9的
(为了可以适用于输入一连串字符通用加了个循环
来看以下代码:
x=text1.text
for i=1 to len (x)
c=asc (mid (x,i,1))
d=(c mod 16)*16+c╲16
y=y+chr(d)
next i
text2.text=y
有更好的想法欢迎提出orz
下次主题:实现输出不重复的随机数或者是讲基础算法((仅适用于入门观望的小白…佬可以无视