一道有趣的面试题

uowzd01 2009-03-26 09:24:17
有关手机短信输入,例如,输入hello,只用按4-3-5-5-6,手机会自动把可能的单词组合显示出来,这样我们不用按4-4(h),3-3(e),5-5-5(l),5-5-5(l),6-6-6(o)。

实现方法:有一个字典文件包含所有单词
apple
astute
boomerang
car
donkey
effort
fun
.......

字母和数字按键相对应
2,abc
3,def
4,ghi
5,jkl
6,mno
7,pqrs
8,tuv
9,wxyz

写一个程序实现输入一串数字得到所有可能的单词(c#)
...全文
353 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
A海阔天空 2009-03-26
  • 打赏
  • 举报
回复

帮顶。
mykelly6 2009-03-26
  • 打赏
  • 举报
回复
用正则表达式阿,按2就是[a-c]
wackyboy 2009-03-26
  • 打赏
  • 举报
回复
试着做的:


string str = @"apple
astute
boomerang
car
donkey
effort
apples
fun "; // 单词库

string[] Key = new string[] {"abc","def","ghi","jkl","mno","pqrs","tuv","wxyz" }; // 键盘按键组

string input = Console.ReadLine();
int count = input.Length;
if (count > 0)
{
string pattern = "^"; // 正则表达式
for (int i = 0; i < count; i++)
{
char c = input[i];
int index = Convert.ToInt32(c.ToString())-2;
pattern += "[" + Key[index] + "]"; // 拼接正则表达式
}
pattern += "[a-z]*"; // 拼接正则表达式
MatchCollection mc = Regex.Matches(str, pattern,RegexOptions.Multiline);
foreach (Match m in mc)
{
Console.WriteLine(m.Value); // 所有符合的结果
}
}
else
{
Console.WriteLine("Input Pls!!");
}
chinesesword 2009-03-26
  • 打赏
  • 举报
回复
我觉得把字典 转换成数字串
输入一串数字后找和他匹配的数字串应该就可以了吧?
chinesesword 2009-03-26
  • 打赏
  • 举报
回复
mark
liqichose 2009-03-26
  • 打赏
  • 举报
回复
写一个类型: mType<int number,list<string>word>
一个生成上面类型中number的方法:
通过字典。将单词解析成chararray,然后就可以得到数字。
将数字在mType实例中查找,如果找到,将该单词加入list
如果没找到,就insert一个新元素。

这样可以吗?
typeof 2009-03-26
  • 打赏
  • 举报
回复
有可能有同一串数字对应多个单词。
wanabe 2009-03-26
  • 打赏
  • 举报
回复
呵呵 数组排列对应
wolf1118baby 2009-03-26
  • 打赏
  • 举报
回复
根据你点击的数值理得字母组合去 存储的单词列表里寻找!找到符合就显示 不符合继续!
lethwei 2009-03-26
  • 打赏
  • 举报
回复
有意思的问题, mark~
有个初步想法, 把字典中的单词按照数字预先编好组, 然后对输入做 hash/二分 查找
zhoulehua 2009-03-26
  • 打赏
  • 举报
回复
不错,顶了。
uowzd01 2009-03-26
  • 打赏
  • 举报
回复
谢谢大家的热心回复,其实关键的地方就是把n个数组排列一遍,chineseword的想法很好,从字典推出数字比较简单,或者用正则表达式的高手们的办法都很妙。但我理解从考官的角度可能是想看你怎样实现得到n个数组的排列组合(逻辑思维能力??)

我用arraylist写了一个,感觉有点笨。

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace MobileInput
{
class Program
{
//生成对应的字母
static ArrayList getAlphabet(int number)
{
ArrayList list = new ArrayList();
switch (number)
{
case 2:
list.Add('a');
list.Add('b');
list.Add('c');
break;
case 3:
list.Add('d');
list.Add('e');
list.Add('f');
break;
case 4:
list.Add('g');
list.Add('h');
list.Add('i');
break;
case 5:
list.Add('j');
list.Add('k');
list.Add('l');
break;
case 6:
list.Add('m');
list.Add('n');
list.Add('o');
break;
case 7:
list.Add('p');
list.Add('q');
list.Add('r');
list.Add('s');
break;
case 8:
list.Add('t');
list.Add('u');
list.Add('v');
break;
case 9:
list.Add('w');
list.Add('x');
list.Add('y');
list.Add('z');
break;
}
return list;
}

//把两个arraylist合并
static ArrayList mergeArray(ArrayList a, ArrayList b)
{
ArrayList list = new ArrayList();
StringBuilder sb = new StringBuilder("");
foreach (object alphabet_one in a)
{
foreach (object alphabet_two in b)
{
sb.Append(alphabet_one).Append(alphabet_two);
list.Add(sb.ToString());
sb.Remove(0, sb.Length);
}
}
return list;
}

//字典
static Hashtable getDictionary()
{
Hashtable ht = new Hashtable();
ht.Add("apple",1);
ht.Add("astute", 1);
ht.Add("boomerang", 1);
ht.Add("car", 1);
ht.Add("donkey", 1);
ht.Add("effort", 1);
ht.Add("fun", 1);
ht.Add("ghastly", 1);
ht.Add("hello", 1);
return ht;
}

static void Main(string[] args)
{
Console.WriteLine("输入数字:(2-9)");
string input = Console.ReadLine();
int len = input.Length;

ArrayList list = new ArrayList();
//开始合并n个arraylist
for (int i = 0; i < len - 1; i++)
{
if (i==0)
list = getAlphabet(Convert.ToInt32(input.Substring(i, 1)));
list = mergeArray(list, getAlphabet(Convert.ToInt32(input.Substring(i + 1, 1))));
}

foreach (object o in list)
{
if (getDictionary().ContainsKey(o))
Console.WriteLine(o);
}
Console.Read();
}
}
}

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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