搜狗面试题

woshayawo 2011-09-17 12:25:18
以下程序是一个信息编码的程序,阅读其encode部分,并补全其decode部分
最后运行程序,会打印出的一句话。这句话就是我们要求的答案。

注意!这句话是用GBK编码的!
答案请复制粘贴, 不要手动输入
每次重新登录时请重新答题


public class Test {


public static void encode(byte[] in, byte[] out, int password)
{
int len = in.length;

int seed = password ^ 0xe84f172f;
for (int i = 0 ; i < len; ++i) {
byte a = (byte)( ( in[i] ^ seed ) >>> 5 );
byte b = (byte)( ( ( ((int)in[i]) << 13 ) ^ seed ) >>> (13-3) );
a &= 0x7;
b &= 0xf8;
out[i] = (byte)(a | b);
seed = (seed * 5393887 + in[i]);
}
}


public static void decode(byte[] in, byte[] out, int password)
{
int len = in.length;

int seed = password ^ 0xe84f172f;
for (int i = 0 ; i < len; ++i) {
// fill the code here
}
}
public static void main(String [] args) throws Exception
{
int password = 0xea5c3443;
byte[] buf1 = {-107, 88, 37, 78, -35, 5, 60, 6, -86, 36, -105, 43, 99, -83, -1, -82, -103, -3, 36, -124, -100, 62, -7, -42, -71, 119, 82, 41, -89, 95, 59, -31, -84, 43, 85, 44, };
byte[] buf2 = new byte[buf1.length];
decode(buf1, buf2, password);
System.out.println(new String(buf2, "GBK"));
}


}
...全文
864 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
darkeyes 2011-09-22
  • 打赏
  • 举报
回复
花了一个小时总算是明白讲的是什么了。。。
woshayawo 2011-09-22
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 sjp524617477 的回复:]

花了一个小时总算是明白讲的是什么了。。。
[/Quote]

把心得跟大家说说呗。
dslpk 2011-09-17
  • 打赏
  • 举报
回复
接分得
打油的程序员 2011-09-17
  • 打赏
  • 举报
回复


public class Test {

public static void encode(byte[] in, byte[] out, int password) {
int len = in.length;

int seed = password ^ 0xe84f172f;
for (int i = 0; i < len; ++i) {
byte a = (byte) ((in[i] ^ seed) >>> 5);
byte b = (byte) (((((int) in[i]) << 13) ^ seed) >>> (13 - 3));
a &= 0x7;
b &= 0xf8;
out[i] = (byte) (a | b);
seed = (seed * 5393887 + in[i]);
}
}

public static void decode(byte[] in, byte[] out, int password) {
int len = in.length;

int seed = password ^ 0xe84f172f;
for (int i = 0; i < len; ++i) {
// fill the code here

byte a = (byte) ((in[i] & 0x07) << 5); // 把密文的第三位变成高三位
//第0位到第2位置0,右移3位,b中第0位到第4位是有效位
byte b = (byte) ((in[i] & 0xf8) >>> 3); // 把密文的高五位变成低五位
a = (byte) (a ^ seed); // 还原
a &= 0xe0; //无效位再次置0,因为有可能,经过异或之后变成1了
b = (byte) (((((int) b) << 13) ^ seed) >>> 13); // 还原
b &= 0x1f; ////无效位再次置0,因为有可能,经过异或之后变成1了

out[i] = (byte) (a | b); //将临时变量中的数据串接,保存到out[i],这是真实的数据来了,
seed = (seed * 5393887 + out[i]);
}
}

public static void main(String[] args) throws Exception {
int password = 0xea5c3443;
byte[] buf1 = { -107, 88, 37, 78, -35, 5, 60, 6, -86, 36, -105, 43, 99,
-83, -1, -82, -103, -3, 36, -124, -100, 62, -7, -42, -71, 119,
82, 41, -89, 95, 59, -31, -84, 43, 85, 44, };
byte[] buf2 = new byte[buf1.length];
decode(buf1, buf2, password);
System.out.println(new String(buf2, "GBK"));
}

}
/*
搜狗输入法皮肤拥有数万款炫酷皮肤!!
*/

TKD03072010 2011-09-17
  • 打赏
  • 举报
回复
晕了!
木子0204 2011-09-17
  • 打赏
  • 举报
回复
看着很晕。

62,629

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧