题目描述
继MIUI8推出手机分身功能之后,MIUI9计划推出一个电话号码分身的功能:首先将电话号码中的每个数字加上8取个位,然后使用对应的大写字母代替 ("ZERO", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE"), 然后随机打乱这些字母,所生成的字符串即为电话号码对应的分身。
输入描述:
第一行是一个整数T(1 ≤ T ≤ 100)表示测试样例数;接下来T行,每行给定一个分身后的电话号码的分身(长度在3到10000之间)。
输出描述:
输出T行,分别对应输入中每行字符串对应的分身前的最小电话号码(允许前导0)。
示例1
输入
4
EIGHT
ZEROTWOONE
OHWETENRTEO
OHEWTIEGTHENRTEO
输出
0
234
345
0345
思路: 找"ZERO", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE"中的特征数,第一次查找:"ZERO"中的z,"TWO"中的w, "FOUR"中的u, "SIX"中的x, "EIGHT"中的g。每找到一个就在字符串中删除当前数字所有字母,这样第二批就可以查找:"ONE"中的o, "THREE"中的t, "FIVE"中的f, "SEVEN"中的s, 最后查找nine。
#include "atlstr.h"
#include <iostream>
using std::cin;
using std::cout;
using std::string;
CString NumName[10] = {"ZERO", "ONE","TWO","THREE","FOUR","FIVE","SIX","SEVEN","EIGHT","NINE"};
#define zerotag NumName[0][0] //z
#define twotag NumName[2][1] //w
#define fourtag NumName[4][2] //u
#define sixtag NumName[6][2] //x
#define eighttag NumName[8][2] //g
CString g_first_check[5] = { NumName[0], NumName[2], NumName[4], NumName[6], NumName[8]};
unsigned char g_tag_first_check[5] = {zerotag, twotag, fourtag, sixtag, eighttag};
unsigned char g_result_first_check[5] = {0};
typedef struct
{
CString NUM;
unsigned char Tag;
unsigned char result;
int number;
}Checklevel;
Checklevel FirstCheck[10] = {
{ NumName[0], zerotag, 0, 0 },
{ NumName[2], twotag, 0, 2 },
{ NumName[4], fourtag, 0, 4 },
{ NumName[6], sixtag, 0, 6 },
{ NumName[8], eighttag, 0, 8 },
{ NumName[1], NumName[1][0], 0, 1 },
{ NumName[3], NumName[3][0], 0, 3 },
{ NumName[5], NumName[5][0], 0, 5 },
{ NumName[7], NumName[7][0], 0, 7 },
{ NumName[9], NumName[9][0], 0, 9}
};
Checklevel SecondCheck[4] = {
{ NumName[1], NumName[1][0], 0, 1 },
{ NumName[3], NumName[3][0], 0, 3 },
{ NumName[5], NumName[5][0], 0, 5 },
{ NumName[7], NumName[7][0], 0, 7 }
};
int Removetag(CString* txt, Checklevel* checklevel, int tagnum)
{
int loc = 0;
int ret = 0;
for (int len = 0; len < strlen(checklevel[tagnum].NUM); len++)
{
loc = (*txt).Find(checklevel[tagnum].NUM[len]);
if (loc < 0)
{
ret = -1;
break;
}
(*txt).Delete(loc, 1);
}
return ret;
}
int SortNum(CString *txt, Checklevel* checklevel, int NumofCheck)
{
int checkNUM = 0;
int ret = 0;
for (checkNUM = 0; checkNUM < NumofCheck; checkNUM++)
{
int lenoftxt = strlen((*txt));
if (strlen((*txt)) == 0)
break;
for (int i = 0; i < strlen((*txt)); )
{
if ((*txt)[i] == checklevel[checkNUM].Tag)
{
checklevel[checkNUM].result++;
ret = Removetag(txt, checklevel, checkNUM);
if (ret < 0)
{
return -1;
}
i = 0;
}
else
{
i++;
}
}
}
return 0;
}
int main()
{
int ret = 0;
int i = 0;
int j = 0;
int k = 0;
int result[10] = {0};
int total = 1;
int linecount = -1;
CString txt;
char** buf = NULL; //动态分配 //char buf[1024]; //指定长度
char ch;
int len = 0;
printf("Please input some line:\n");
scanf_s("%d", &total); //%c是字符 %s是字符串
if (total > 0)
{
buf = (char **)malloc(total * sizeof(char *));
}
for (i = 0; i < total; i++)
{
buf[i] = (char *)malloc(sizeof(char)* 1);
}
while (scanf_s("%c", &ch) == 1 && ch != EOF && total >= 0){ //定义一个结束符或者ctrl+z/d ch != '#'
//动态的实现
if (ch == 0xa)
{
linecount++;
total--;
len = 0;
if (total < 0)
{
break;
}
continue;
}
buf[linecount] = (char*)realloc(buf[linecount], sizeof(char)*(len + 2));
buf[linecount][len++] = ch;
buf[linecount][len] = NULL; //只有以NULL结尾的char[]才能强制转换为CString
//buf[len++] = ch; //指定长度
}
for (i = 0; i < linecount; i++)
{
CString testtxt;
testtxt = buf[i];
testtxt.Format("%s", buf[i]);
do{
ret = SortNum(&testtxt, FirstCheck, 10);
if (ret < 0)
break;
} while (0);
for (j = 0; j < 10; j++)
{
int loc = (j % 2) * 5 + j / 2;
result[j] = FirstCheck[loc].result;
FirstCheck[loc].result = 0;
for (k = 0; k < result[j]; k++)
{
printf("%d", j);
}
}
printf("\n");
}
system("pause");
return ret;
}