字符串水题, 但是最后一步关于计算重复度的还是很好玩的地方。
另外就是注意代码简洁性、可读性的练习。
#include <iostream> #include <vector> #include <algorithm> using namespace std; char getNumber(char c){ if(c=='-' or c=='Q' or c=='Z') return '-'; if(c>='0' and c<='9') return c; if(c=='S') return '7'; if(c=='V') return '8'; if(c=='Y') return '9'; if(c>='A' and c<='Z') return (c-'A')/3 + 2 + '0'; return '-'; } struct PhoneNumber { string s; void format(string tmp){ s=""; for (int i = 0; i < tmp.length(); ++i) { char t = getNumber(tmp[i]); if(t!='-') s += t; } } void print(){ for (int i = 0; i < 3; ++i) { cout<<s[i]; } cout<<"-"; for (int i = 3; i < s.length(); ++i) { cout<<s[i]; } } }; int n; void init(){ cin>>n; } PhoneNumber book[100000]; bool cmp_pn(const PhoneNumber& a, const PhoneNumber& b){ return a.s < b.s; } void build(){ string tmp; for (int i = 0; i < n; ++i) { cin>>tmp; book[i].format(tmp); } sort(book,book+n,cmp_pn); } void output(){ int cur = 0; int cur_dp = 1; bool no_dup = true; for (int i = 1; i < n; ++i) { if(book[i].s == book[cur].s){ cur_dp++; if(cur_dp>=2) no_dup = false; }else{ if(cur_dp>=2){ book[cur].print(); cout<<" "<<cur_dp<<endl; } cur_dp = 1; cur = i; } } if(no_dup) cout<<"No duplicates."<<endl; } int main(int argc, char const *argv[]) { init(); build(); output(); return 0; }