关于搜狗在线测评上面的题目详解

前几天在投递了搜狗Java开发工程师。需要搞一个在线测评,出了一道题目,是程序填空题

要求观察程序,补全decode,输出一句话:

public  class  Test  { 


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

	int  seed  =  password  ^  0x1963f9c; 
	for  (int  i  =  0  ;  i  <  len;  ++i)  { 
	byte  a  =  (byte)(  (  in[i]  ^  seed  )  >>>  2  ); 
	byte  b  =  (byte)(  (  (  ((int)in[i])  <<  13  )  ^  seed  )  >>>  (13-6)  ); 
	a  &=  0x3f; 
	b  &=  0xc0; 
	out[i]  =  (byte)(a  |  b); 
	seed  =  (((seed  <<  7)  ^  seed  ^  in[i])  +  5393887); 
	} 
	} 


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

	int  seed  =  password  ^  0x1963f9c; 
	for  (int  i  =  0  ;  i  <  len;  ++i)  { 
        byte a = (byte)(in[i] & 0x3f);  
        byte b = (byte)(in[i] & 0xc0);
        a = (byte)(((a << 2) ^ seed) & 0xfc);
        b = (byte)((((((int)b) << (13-6)) ^ seed) >>13) & 0x03); 
		out[i]  =  (byte)(a  |  b);  
		seed  =  (((seed  <<  7)  ^  seed  ^  out[i])  +  5393887); 

	} 
	} 
	public  static  void  main(String  []  args)  throws  Exception 
	{ 
	int  password  =  0x99b90094; 
	byte[]  buf1  =  {113,  87,  1,  -29,  55,  -5,  65,  13,  -85,  78,  50,  23,  -33,  -29,  73,  117,  -97,  106,  77,  -17,  -107,  -62,  -110,  -126,  -15,  32,  -48,  -128,  }; 
	byte[]  buf2  =  new  byte[buf1.length]; 
	decode(buf1,  buf2,  password); 
	System.out.println(new  String(buf2,  "GBK")); 
	} 


	} 


 

思前想后,发现规律。这就是一系列的移位,我们要做的就是反过来再移一次。先看看encode方法,然后再去实现decode。方法如下:

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

	int  seed  =  password  ^  0x1963f9c; 
	for  (int  i  =  0  ;  i  <  len;  ++i)  { 
        byte a = (byte)(in[i] & 0x3f);  
        byte b = (byte)(in[i] & 0xc0);
        a = (byte)(((a << 2) ^ seed) & 0xfc);
        b = (byte)((((((int)b) << (13-6)) ^ seed) >>13) & 0x03); 
		out[i]  =  (byte)(a  |  b);  
		seed  =  (((seed  <<  7)  ^  seed  ^  out[i])  +  5393887); 

	} 
	} 


最后输出的结果是:听歌从搜狗音乐搜索开始!!!


 

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值