Created
September 3, 2011 21:50
-
-
Save flying19880517/1191843 to your computer and use it in GitHub Desktop.
五笔编码索引转换
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
namespace WubiCodeIndex | |
{ | |
class Program | |
{ | |
//题目: | |
//五笔的编码范围是a ~ y的25个字母,从1位到4位的编码,如果我们把五笔的编码按字典序排序,形成一个数组如下: | |
//a, aa, aaa, aaaa, aaab, aaac, … …, b, ba, baa, baaa, baab, baac … …, yyyw, yyyx, yyyy | |
//其中a的Index为0,aa的Index为1,aaa的Index为2,以此类推。 | |
//1)编写一个函数,输入是任意一个编码,比如baca,输出这个编码对应的Index; | |
//2)编写一个函数,输入是任意一个Index,比如12345,输出这个Index对应的编码。 | |
static int m4 = 25 * 25 * 25 + 25 * 25 + 25 + 1; | |
static int m3 = 25 * 25 + 25 + 1; | |
static int m2 = 25 + 1; | |
static int m1 = 1; | |
static void Main(string[] args) | |
{ | |
string code = "asya"; | |
int index = 12345; | |
Console.WriteLine("五笔编码:" + code); | |
Console.WriteLine("索引:" + GetCodeIndex(code)); | |
Console.WriteLine("五笔编码索引:" + index); | |
Console.WriteLine("编码:" + GetCode(index)); | |
} | |
static int GetCodeIndex(string code) | |
{ | |
int index = 0; | |
if (code.Length > 0) | |
index += (code[0] - 'a') * m4 + 1; | |
if (code.Length > 1) | |
index += (code[1] - 'a') * m3 + 1; | |
if (code.Length > 2) | |
index += (code[2] - 'a') * m2 + 1; | |
if (code.Length > 3) | |
index += (code[3] - 'a') * m1 + 1; | |
return index - 1; | |
} | |
static string GetCode(int index) | |
{ | |
index++; | |
index -= 1; | |
string code = ((char)('a' + (index / m4))).ToString(); | |
index %= m4; | |
if (index > 0) | |
{ | |
index -= 1; | |
code += (char)('a' + (index / m3)); | |
index %= m3; | |
if (index > 0) | |
{ | |
index -= 1; | |
code += (char)('a' + (index / m2)); | |
index %= m2; | |
if (index > 0) | |
{ | |
index -= 1; | |
code += (char)('a' + (index / m1)); | |
} | |
} | |
} | |
return code; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment