标签:
输入包含多组数据。 每组数据第一行包含一个正整数n(1≤n≤1024)。 紧接着n行,每行包含一个电话号码,电话号码仅由连字符“-”、数字和大写字母组成。 没有连续出现的连字符,并且排除连字符后长度始终为7(美国电话号码只有7位)。输出描述:
对应每一组输入,按照字典顺序输出不重复的标准数字形式电话号码,即“xxx-xxxx”形式。 每个电话号码占一行,每组数据之后输出一个空行作为间隔符。输入例子:
12 4873279 ITS-EASY 888-4567 3-10-10-10 888-GLOP TUT-GLOP 967-11-11 310-GINO F101010 888-1200 -4-8-7-3-2-7-9- 487-3279 4 UTT-HELP TUT-GLOP 310-GINO 000-1213输出例子:
310-1010 310-4466 487-3279 888-1200 888-4567 967-1111 000-1213 310-4466 888-4357 888-4567// write your code here cpp #include <iostream> #include <string.h> #include <map> using namespace std; bool IsOk(char *str) { int count = 0; int n = strlen(str); char *p = str; while (*p != '\0') { if (*p == '-') count++; p++; } if (n - count != 7)return false; return true; } bool isNum(char ch) { return (ch - '0') >= 0 && (ch - '0') <= 9; } int main() { int n; char inputStr[1024]; char StrBuff[256] = {0}; StrBuff['A'] = '2'; StrBuff['B'] = '2'; StrBuff['C'] = '2'; StrBuff['D'] = '3'; StrBuff['E'] = '3'; StrBuff['F'] = '3'; StrBuff['G'] = '4'; StrBuff['H'] = '4'; StrBuff['I'] = '4'; StrBuff['J'] = '5'; StrBuff['K'] = '5'; StrBuff['L'] = '5'; StrBuff['M'] = '6'; StrBuff['N'] = '6'; StrBuff['O'] = '6'; StrBuff['P'] = '7'; StrBuff['Q'] = '7'; StrBuff['R'] = '7'; StrBuff['S'] = '7'; StrBuff['T'] = '8'; StrBuff['U'] = '8'; StrBuff['V'] = '8'; StrBuff['W'] = '9'; StrBuff['X'] = '9'; StrBuff['Y'] = '9'; StrBuff['Z'] = '9'; StrBuff['-'] = '-'; //cin >> n; while (cin>>n) { map<string, int> mp; while (n--) { cin >> inputStr; string s; if (IsOk(inputStr)) { char *p = inputStr; while (*p != '\0') { if (s.size() == 3) { s += '-'; continue; } else { if (isNum(*p)) { s += *p; } else { if (*p != '-') s += StrBuff[*p]; } } p++; } mp.insert(pair<string, int>(s, 0)); } } map<string, int> ::iterator it = mp.begin(); while (it != mp.end()) { cout << (*it).first.c_str() << endl; it++; } cout << endl; } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/liuhuiyan_2014/article/details/47683171