以下题目的答案, 我在评论中,给出了。
大家有什么疑问就提出来吧,哎,本人不才,这些题目,都是类似的。在线评测就一道题,要求半小时
//题目1
/*
**以下程序是一个信息编码程序,阅读器encode部分,并补全decode部分
**最后运行程序,会打印出一句话,这句话就是我们要求的答案。
**注意:这句话是GBK编码的!
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
typedef unsigned int uint_32;
typedef unsigned char byte;
void encode(const void* raw_in, void* raw_out, uint_32 password, uint_32 len)
{
const byte* in = (const byte*)raw_in;
byte* out = (byte*)raw_out;
uint_32 seed = password ^ 0x9394d4e9u;
uint_32 i;
for (i = 0 ; i < len; ++i)
{
byte a = ( in[i] ^ seed ) >> 1;
byte b = ( ( ((uint_32)in[i]) << 13 ) ^ seed ) >> (13-7);
a &= 127;
b &= 128;
a = 127 & ( a ^ (b << 3));
out[i] = a | b;
seed = (((seed << 7) ^ seed ^ out[i]) + 48475829);
}
}
void decode(const void* raw_in, void* raw_out, uint_32 password, uint_32 len)
{
const byte* in = (const byte*)raw_in;
byte* out = (byte*)raw_out;
uint_32 seed = password ^ 0x9394d4e9u;
uint_32 i;
for (i = 0 ; i < len; ++i) {
// 请在此处补全代码
}
}
int main()
{
const byte buf1[] = {0x9c, 0xf9, 0x5b, 0x1f, 0xb9, 0x86, 0x4e, 0x39, 0xce,
0xec, 0x31, 0xbc, 0x31, 0xde, 0xc4, 0x3d, 0xfe, 0x56,
0xed, 0xdf, 0xc1, 0x4c, 0x07, 0xa8, 0x23, 0xdc, 0xdc,
0x97, 0x48, 0xc7, 0x5f, 0xeb, 0xb9, 0x1e };
byte buf2[100] = {0};
const uint_32 password = 0x8f98ce1bu;
const uint_32 len = sizeof(buf1);
decode(buf1, buf2, password, len);
printf("%s\n", buf2);
}
第二题:(java版的,不过对于C++人来说,一切都是小菜。。。YY)
public class Test {
public static void encode(byte[] in, byte[] out, int password) {
int len = in.length;
int seed = password ^ 0xd7f59877;
for (int i = 0; i < len; ++i) {
byte a = (byte) ((in[i] ^ seed) >>> 4);
byte b = (byte) (((((int) in[i]) << 13) ^ seed) >>> (13 - 4));
a &= 0xf;
b &= 0xf0;
out[i] = (byte) (a | b);
seed = (((seed << 7) ^ seed ^ in[i]) + 608347);
}
}
public static void decode(byte[] in, byte[] out, int password) {
int len = in.length;
int seed = password ^ 0xd7f59877;
for (int i = 0; i < len; ++i) {
//补全这部分
}
}
public static void main(String[] args) throws Exception {
int password = 0x9bda1406;
byte[] buf1 = { 123, -44, -22, -125, -3, -128, 107, 7, 81, -47, -5,
-122, -73, -89, 118, -114, -89, 6, 126, -23, 124, -1, 25, 13,
-122, 55, -95, -21, -22, 80, -101, 29, -119, 60, 54, 57, 91,
14, -82, 114, -4, -122, 95, 1, 21, -96, 98, 43, -62, 92, 74,
-55, -51, -87, 21, 16, 12, 40, 84, -109, };
byte[] buf2 = new byte[buf1.length];
decode(buf1, buf2, password);
System.out.println(new String(buf2, "GBK"));
}
}
第三题
//第三题:
//本题来源:大家可以参考下
//http://bbs.chinaunix.net/thread-3593303-1-1.html
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#in