北大acm1002

这道题,我也一直在报错runtime error,这种问题主要是因为数组越界,溢出

注意,电话号码可能是0000033这种0开头的转化成数字时,前面的0会消失变为33,这样在给数字最后加‘-’时就会越界报runtime error

下面是我的代码

#include<iostream>
#include<map>
#include<string>
#include<fstream>
#include<vector>
#include<algorithm>
#include<sstream>
#include<cstring>
using namespace std;




int main()
{
	//ifstream fin("C:\\Users\\ZSM\\Desktop\\now\\test\\input1002.txt");
	int n;
	cin >> n;
	string s;
	vector<long> list;
	if (n == 0)
	{
		cout << "No duplicates.";
		return 0;
	}
	while (n--)
	{
		cin >> s;
		string str="";
		for (int i = 0; i < s.size(); i++)
		{
			if (s[i] == '-')
				continue;
			else if (s[i] == 'A' || s[i] == 'B' || s[i] == 'C')
				str.push_back('2');
			else if (s[i] == 'D' || s[i] == 'E' || s[i] == 'F')
				str.push_back('3');
			else if (s[i] == 'G' || s[i] == 'H' || s[i] == 'I')
				str.push_back('4');
			else if (s[i] == 'J' || s[i] == 'K' || s[i] == 'L')
				str.push_back('5');
			else if (s[i] == 'M' || s[i] == 'N' || s[i] == 'O')
				str.push_back('6');
			else if (s[i] == 'P' || s[i] == 'R' || s[i] == 'S')
				str.push_back('7');
			else if (s[i] == 'T' || s[i] == 'U' || s[i] == 'V')
				str.push_back('8');
			else if (s[i] == 'W' || s[i] == 'X' || s[i] == 'Y')
				str.push_back('9');
			else if (s[i] >= '0'&&s[i] <= '9')
				str.push_back(s[i]);
		}
		long num = atoi(str.c_str());
		list.push_back(num);
	}
	sort(list.begin(),list.end());
	map<long, long>num;
	map<long, long>::iterator it;
	int i = 0;
	vector<long>temp;
	while (i < list.size())
	{
		while (i + 1<list.size()&&list[i] == list[i + 1])
		{
			it = num.find(list[i]);
			if (it == num.end())//没找到
			{
				num[list[i]] = 2;
				temp.push_back(list[i]);
			}
			else
				num[list[i]]++;
			i++;
		}
			i++;
	}
	if (temp.size() == 0)
	{
		cout << "No duplicates.";
	}
	else
	{
		for (int i = 0; i < temp.size(); i++)
		{
			ostringstream oss;
			string str;
			oss << temp[i];
			str = oss.str();
			if (str.size() != 7)
			{
				int m=7 - str.size();
				while (m--)
				{
					str.insert(str.begin(), '0');
				}
			}
			str.insert(str.begin() + 3, '-');
			cout << str << ' ' << num[temp[i]] << endl;
		}
	}
	system("pause");
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值