返回信息流首先说明本题目的原意:以下程序是GBK编码和解码程序,根据编码过程encode(),将解码过程decode()补充完整。写出输出结果,输出结果是一句话。
public class Test {
public static void encode(byte[] in, byte[] out, int password)
{
int len = in.length;
int seed = password ^ 0x2c8f7672;
for (int i = 0 ; i < len; ++i) {
byte a = (byte)( ( in[i] ^ seed ) >>> 2 );
byte b = (byte)( ( ( ((int)in[i]) << 10 ) ^ seed ) >>> (10-6) );
a &= 0x3f;
b &= 0xc0;
out[i] = (byte)(a | b);
seed = (((seed << 7) ^ seed ^ out[i]) + 536513);
}
}
public static void decode(byte[] in, byte[] out, int password)
{
int len = in.length;
int seed = password ^ 0x2c8f7672;
for (int i = 0 ; i < len; ++i) {
//……
}
}
public static void main(String [] args) throws Exception
{
int password = 0xa15ab37a;
byte[] buf1 = {-80, -86, -85, 77, 23, -94, 2, 77, 111, -35, -60, 68, -62, -128, -113, 27, 84, 11, 0, 61, 13, -43, 56, -50, 39, 55, -99, 114, -28, 104, -65, 91, 66, -97, 52, 80, -109, -6, 11, 29, -14, 98, -16, -95, 38, 37, 122, -75, 72, -54, -56, -86, -112, -28, };
byte[] buf2 = new byte[buf1.length];
decode(buf1, buf2, password);
System.out.println(new String(buf2, "GBK"));
}
}
投得职位不要求JAVA,却要求做JAVA类的题目~没办法,来求助下。谢谢
输出的文字应该是:搜狗XXX之类的
这是一条镜像帖。来源:北邮人论坛 / java / #20040同步于 2011/9/12
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Java机器人发帖
搜狗校招JAVA类题目求解
psdx
2011/9/12镜像同步11 回复
订阅后,新回复会通过你的通知中心匿名送达。
9 条回复
public static void decode(byte[] in, byte[] out, int password) {
int len = in.length;
int seed = password ^ 0x2c8f7672;
for (int i = 0; i < len; ++i) {
byte a = (byte) (((in[i] << 2)^seed));
byte b = (byte) (((((int) in[i]) << 4) ^ seed) >>> 10);
a &= 0xfc;
b &= 0x03;
out[i] = (byte) (a | b);
seed = (((seed << 7) ^ seed ^ in[i]) + 536513);
}
}
编码的方法是将输入字节的高6位编码后无符号右移到低6位,而将低2位先左移10位进行编码再移到字节的高2位,解码只要将这两段信息分别进行移位,解码,就可以了。
解码利用的是异或的性质 a^b^b=a,再和seed异或一次就可以恢复信息
输出的信息是 “搜狗已成为中文互联网领域最具发展潜力的公司之一!!!!”