记录一下最近做过的一些面试算法题目
程序员文章站
2024-02-02 13:46:46
...
题目
6 进制转成10 进制
‘a’ = 0, ‘e’ = 1, ‘i’ = 2, ‘o’=3, ‘u’=4, ‘g’=5
写一个函数 int Convert(string str)
输入的str: 比如 “ei” 输出为 8
“e” 输出为1
“ioa” 输出为90
static int Convert(string str)
{
//将字母转换成对应的数字,6进制转十进制
char []ch= str.ToCharArray();
int result = 0;
for (int i = 0; i < ch.Length; i++)
{
switch (ch[i])
{
case 'a':ch[i]='a';break;
case 'e':ch[i]='b';break;
case 'i': ch[i] = 'c'; break;
case 'o': ch[i] = 'd'; break;
case 'u': ch[i] = 'e'; break;
case 'g': ch[i] = 'f'; break;
default:
break;
}
}
int j = 0;
for (int i = ch.Length-1; i >= 0; i--)
{ //对应数字乘6的次幂相加
result += (int)((ch[i] - 97) * Math.Pow(6, j));
j++;
}
Console.WriteLine(result);
Console.ReadLine();
return result;
}
测试用例
static void Main(string[] args)
{
string str="ioa";
Convert(str);
}
上一篇: 记录一些最近做过的题目
下一篇: 统计字母在字符串中出现的次数