码迷,mamicode.com
首页 > 其他好文 > 详细

编程马拉松:电话号码

时间:2015-08-15 18:26:38      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:

题目描述

技术分享
上图是一个电话的九宫格,如你所见一个数字对应一些字母,因此在国外企业喜欢把电话号码设计成与自己公司名字相对应。例如公司的Help Desk号码是4357,因为4对应H、3对应E、5对应L、7对应P,因此4357就是HELP。同理,TUT-GLOP就代表888-4567、310-GINO代表310-4466。
NowCoder刚进入外企,并不习惯这样的命名方式,现在给你一串电话号码列表,请你帮他转换成数字形式的号码,并去除重复的部分。

输入描述:
输入包含多组数据。

每组数据第一行包含一个正整数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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!