- 如果相鄰號碼相同
- 次數times前後累加
- 如果相鄰號碼不同
- 上一個號碼次數不爲1:輸出
- 上一個號碼次數爲1:不輸出
注意:以上邏輯總不會顯示(輸入的)最後一個重複號碼,要單獨判斷
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
char arr[26]={'2','2','2',
'3','3','3',
'4','4','4',
'5','5','5',
'6','6','6',
'7','7','7','7',
'8','8','8',
'9','9','9','9'};
struct phoneNum
{
phoneNum(string str="",long time=1){
this->num=str;
this->times=time;
};
string num;
long times;
};
void handleStr(string& str);
bool comp(const phoneNum& n1,const phoneNum& n2){
return n1.num<n2.num;
}
int main()
{
long n;
cin>>n;
std::vector<phoneNum> v(n);
string str;
for (int i = 0; i < n; ++i)
{
cin>>v[i].num;
}
for (int i=0;i<n;i++)
{
handleStr(v[i].num);
}
sort(v.begin(),v.end(),comp);
bool have=0;
for (int i=1;i<n;i++)
{
if (v[i].num==v[i-1].num)
{
have=1;
v[i].times=v[i].times+v[i-1].times;
}
else if(v[i-1].times!=1){
cout<<v[i-1].num<<' '<<v[i-1].times<<endl;
}
}
if (have!=1)
{
cout<<"No duplicates."<<endl;
}
// 如果所有數字都相同,以上邏輯有錯
if (v[n-1].times!=1)
{
cout<<v[n-1].num<<" "<<v[n-1].times<<endl;
}
return 0;
}
void handleStr(string& str)
{
for (int i=0;i<str.size();i++)
{
if (str[i]=='-')
{
str.erase(i,1);
i--;
}
else if(!('0'<=str[i]&&'9'>=str[i])){
str[i]=arr[int(str[i]-'A')];
}
}
str.insert(3,"-");
}