论坛首页 招聘求职论坛

今天面试的一道题

浏览 8762 次
精华帖 (0) :: 良好帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2011-08-08  
The Encryption:

The encryption would need to follow below rules and each rule would be applied once in the following order:

ASCII Shifting:   For any Hex ASCII character within the string, let's suppose its current position index at the string is n (zero based), it should be converted to the hex in the above table that is in the n-th position right after current hex character. If the converted character overflows  ‘F', it should circle back from ‘0'.
For example,   “3A4E”   should be translated  to “3B61” because

character ‘3' is in 0th position, it should remain same.
character ‘A' is in 1st position, it should be converted to  ‘B' (1st  Hex after ‘A' at the Hex table.  A - > B)
character ‘4' is in 2nd position, it should be converted to  ‘6' (2nd Hex after ‘4' at the Hex table. 4 –> 5, 6)
character ‘E' is in 3rd position, it should be converted to  ‘1' (3rd Hex after ‘E' at the Hex table, note it overflows ‘F' hence circle back from beginning. E -> F, 0, 1)
Re-ordering: Revert the shifted string.
For example  “3B61” should be re-ordered as “16B3”

Please implement BOTH Encrypt and Decrypt functions and their unit testing code according to the above

没做出来
   发表时间:2011-08-09  
#include <iostream>
#include <cstring>
using namespace std;
const char table[] = {'0', '1', '2', '3', '4', '5',
	'6', '7', '8', '9', 'A', 'B','C', 'D', 'E', 'F'};

int hex2oct(char hex)
{
	if (hex>='0' && hex<= '9')
		return hex-'0';
	else if (hex>='A' && hex<='F')
		return hex-'A'+10;
	else
		return 0;
}

char* encode(const char* plain, char* cipher)
{
	int len = strlen(plain);
	for (int i = 0; i < len; ++i)
	{
		cipher[len-i-1] = table[(hex2oct(plain[i])+i)%16];
	}
	cipher[len] = '\0';
	return cipher;
}

char* decode(const char* plain, char* cipher)
{
	int len = strlen(plain);
	for (int i = 0; i < len; ++i)
	{
		int modreverse = 16-(len-i-1)%16; 
		cipher[len-i-1] = table[(hex2oct(plain[i])+modreverse)%16];
	}
	cipher[len] = '\0';
	return cipher;
}
int main(int argc, char **argv)
{
	char buf[1024];
	char buf1[1024];
	cout << encode("3A4E", buf) << endl;
	cout << decode(encode("3A4E", buf), buf1)  << endl;
	return 0;
}

 不知道你是哪里不懂。。。

0 请登录后投票
   发表时间:2011-08-09  
想到一种比较巧妙的方法:
public class Encryption {
	public static void main(String[] args) {
		System.out.println(Decrypt2(encrypt2("3A4E46ABAE829")));
		System.out.println(encrypt2("3A4E"));
		System.out.println(Decrypt2("16B3"));
	}
	
	//encrypt 加密
	public static String encrypt2(String hexStr){
		String template = "0123456789ABCDEF";
		int len = hexStr.length();
		char[] result = new char[len];
		int index = 0;
		int ch = 0;
		for(int i=0;i<len;i++){
			ch = hexStr.charAt(i);
			index = (i + template.indexOf(ch))%16;
			result[i] = template.charAt(index);
		}
		result = reverse(result);
		return new String(result);
	}
	
	//Decrypt 解密
	public static String Decrypt2(String hexStr){
		String template = "FEDCBA9876543210";
		char[] argStr = hexStr.toCharArray();
		argStr = reverse(argStr);

		char[] result = new char[argStr.length];
		int index = 0;
		for(int i=0;i<argStr.length;i++){
			index = (i + template.indexOf(argStr[i]))%16;
			result[i] = template.charAt(index);
		}
		return new String(result);
	}
	
	//reverse the char array
	public static char[] reverse(char[] chs){
		char temp;
		for(int i=0;i<chs.length/2;i++){
			temp = chs[i];
			chs[i] = chs[chs.length-1-i];
			chs[chs.length-1-i] = temp;
		}
		return chs;
	}
}
0 请登录后投票
   发表时间:2011-08-09  
都是强人,小的连题目都没看明白!伤不起啊。。。。囧
0 请登录后投票
   发表时间:2011-08-09  
那个rerverse函数可以更简洁一点,return new StringBuffer(new String(chs)).reverse().toString().toCharArray(); 一句话即可。
0 请登录后投票
   发表时间:2011-08-09  
是不是英文没看懂啊……我觉得没什么问题啊
0 请登录后投票
   发表时间:2011-08-09  
不懂外语!!!
0 请登录后投票
   发表时间:2011-08-09  
不懂鹰文撒、、鸭梨好大啊
0 请登录后投票
   发表时间:2011-08-09  
这个题还蛮简单的啊!
/**
	 * 加密
	 * @param str 要加密的字符串
	 */
	public static String encrypt (String str) {
		String target = "0123456789ABCDEF";
		char[] ch = new char[str.length()];
		char[] tar = new char[str.length()];
		for(int i=0;i<str.length();i++) {
			ch[i] = target.charAt((target.indexOf(str.charAt(i)) + i)%16);
		}
		int j=0;
		for(int i=ch.length-1;i>=0;i--) {
			tar[j++] = ch[i];
		}
		return new String(tar);
	}
	/**
	 * 解密
	 * @param str 要解密的字符串d
	 */
	public static String decrypt(String str) {
		String target = "0123456789ABCDEF";
		char[] ch = new char[str.length()];
		char[] tar = new char[str.length()];
		int j=0;
		for(int i=str.length()-1;i>=0;i--) {
			ch[j++] = str.charAt(i);
		}
		for(int i=0;i<str.length();i++) {
			tar[i] = target.charAt((target.indexOf(ch[i]) - i)>=0 ? (target.indexOf(ch[i]) - i) : (target.indexOf(ch[i])+16 - i));
		}
		return new String(tar);
	}
0 请登录后投票
   发表时间:2011-08-09  
harry_bote 写道
那个rerverse函数可以更简洁一点,return new StringBuffer(new String(chs)).reverse().toString().toCharArray(); 一句话即可。

还真没想到这个~~~学习了!
0 请登录后投票
论坛首页 招聘求职版

跳转论坛:
Global site tag (gtag.js) - Google Analytics