小米面试题:手机分身,电话号码隐藏。
Posted zdtiio
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了小米面试题:手机分身,电话号码隐藏。相关的知识,希望对你有一定的参考价值。
小米面试题:
手机分身,电话号码隐藏。
0-9分别对应ZERO,ONE,TWO,...,NINE
为保证手机拨号安全性,拨号后,存为对应的字符串,并打乱顺序,保证安全性。
现在给定一个字符串,求最小的数字组合
例:
TWOONE
OTNWOE
12
12
分析:
关键字搜索: | 第一次 | 第二次 | 第三次 |
0 ZERO | Z | ||
1 ONE | O | ||
2 TWO | W | ||
3 THREE | R | ||
4 FOUR | U | ||
5 FIVE | F | ||
6 SIX | X | ||
7 SENVE | S | ||
8 EIGHT | G | ||
9 NINE | I/E |
代码实现如下:
1 #include "stdafx.h" 2 #include<string> 3 #include<iostream> 4 using namespace std; 5 6 int main() 7 { 8 int zidian[26],number[10]; 9 memset(zidian,0,26*sizeof(int)); 10 memset(number,0,10*sizeof(int)); 11 string numberString; 12 while(cin >> numberString) 13 {//while循环,无限获取测试用例 14 for (int i = 0; i < numberString.size(); i++) 15 {//for循环建立字典索引,直接针对字典查,而不再单个搜索。 16 char tempChar = numberString[i]; 17 zidian[(int)tempChar - 65] += 1; 18 } 19 int tempNum; 20 //查找0 ZERO Z 21 if(zidian[(int)\'Z\' - 65] != 0) 22 { 23 tempNum = zidian[(int)\'Z\' - 65]; 24 zidian[(int)\'Z\' - 65] = 0; 25 zidian[(int)\'E\' - 65] -= tempNum; 26 zidian[(int)\'R\' - 65] -= tempNum; 27 zidian[(int)\'O\' - 65] -= tempNum; 28 number[0] = tempNum; 29 } 30 //查找2 TWO W 31 if(zidian[(int)\'W\' - 65] != 0) 32 { 33 tempNum = zidian[(int)\'W\' - 65]; 34 zidian[(int)\'W\' - 65] = 0; 35 zidian[(int)\'T\' - 65] -= tempNum; 36 zidian[(int)\'O\' - 65] -= tempNum; 37 number[2] = tempNum; 38 } 39 //查找4 FOUR U 40 if(zidian[(int)\'U\' - 65] != 0) 41 { 42 tempNum = zidian[(int)\'U\' - 65]; 43 zidian[(int)\'U\' - 65] = 0; 44 zidian[(int)\'F\' - 65] -= tempNum; 45 zidian[(int)\'R\' - 65] -= tempNum; 46 zidian[(int)\'O\' - 65] -= tempNum; 47 number[4] = tempNum; 48 } 49 //查找6 SIX X 50 if(zidian[(int)\'X\' - 65] != 0) 51 { 52 tempNum = zidian[(int)\'X\' - 65]; 53 zidian[(int)\'X\' - 65] = 0; 54 zidian[(int)\'S\' - 65] -= tempNum; 55 zidian[(int)\'I\' - 65] -= tempNum; 56 number[6] = tempNum; 57 } 58 //查找8 EIGHT G 59 if(zidian[(int)\'G\' - 65] != 0) 60 { 61 tempNum = zidian[(int)\'G\' - 65]; 62 zidian[(int)\'G\' - 65] = 0; 63 zidian[(int)\'E\' - 65] -= tempNum; 64 zidian[(int)\'I\' - 65] -= tempNum; 65 zidian[(int)\'H\' - 65] -= tempNum; 66 zidian[(int)\'T\' - 65] -= tempNum; 67 number[8] = tempNum; 68 } 69 //查找1 ONE O 70 if(zidian[(int)\'O\' - 65] != 0) 71 { 72 tempNum = zidian[(int)\'O\' - 65]; 73 zidian[(int)\'O\' - 65] = 0; 74 zidian[(int)\'N\' - 65] -= tempNum; 75 zidian[(int)\'E\' - 65] -= tempNum; 76 number[1] = tempNum; 77 } 78 //查找3 THREE R 79 if(zidian[(int)\'R\' - 65] != 0) 80 { 81 tempNum = zidian[(int)\'R\' - 65]; 82 zidian[(int)\'R\' - 65] = 0; 83 zidian[(int)\'T\' - 65] -= tempNum; 84 zidian[(int)\'H\' - 65] -= tempNum; 85 zidian[(int)\'E\' - 65] -= 2 * tempNum; 86 number[3] = tempNum; 87 } 88 //查找5 FIVE F 89 if(zidian[(int)\'F\' - 65] != 0) 90 { 91 tempNum = zidian[(int)\'F\' - 65]; 92 zidian[(int)\'F\' - 65] = 0; 93 zidian[(int)\'I\' - 65] -= tempNum; 94 zidian[(int)\'V\' - 65] -= tempNum; 95 zidian[(int)\'E\' - 65] -= tempNum; 96 number[5] = tempNum; 97 } 98 //查找7 SEVEN S 99 if(zidian[(int)\'S\' - 65] != 0) 100 { 101 tempNum = zidian[(int)\'S\' - 65]; 102 zidian[(int)\'S\' - 65] = 0; 103 zidian[(int)\'E\' - 65] -= 2 * tempNum; 104 zidian[(int)\'V\' - 65] -= tempNum; 105 zidian[(int)\'N\' - 65] -= tempNum; 106 number[4] = tempNum; 107 } 108 //查找9 NINE I 109 if(zidian[(int)\'I\' - 65] != 0) 110 { 111 tempNum = zidian[(int)\'I\' - 65]; 112 zidian[(int)\'I\' - 65] = 0; 113 zidian[(int)\'N\' - 65] -= 2 * tempNum; 114 zidian[(int)\'E\' - 65] -= tempNum; 115 number[4] = tempNum; 116 } 117 //转化为字符串输出(保证最小) 118 string sum; 119 for (int i = 0; i < 10; i++) 120 { 121 string temps(number[i],(char)(i+48)); 122 sum += temps; 123 } 124 cout << sum << endl; 125 } 126 return 0; 127 }
以上是关于小米面试题:手机分身,电话号码隐藏。的主要内容,如果未能解决你的问题,请参考以下文章