POJ 1002 电话号码字符串处理

这题比较简单,就是对电话号码的字符串处理并排序输出,但是实现时有几个注意的地方

1、用map存储比较方便,可以自动根据string的排序规则排序,而且题目数据恰好是<string,int>的K-V存储特点。

2、用另一个字符串重新拼接待输出地字符串比直接对输入字符串进行替换移位等操作要方便。

3、注意字符和数字的ASCII的转化。

4、‘-’的加入可以在输出的时候,输到第4个字符的时候先输出‘-’。

5、map,set,vector,deque,list等容器要熟练使用,可以参考http://www.cplusplus.com/和MSDN的文档。

#include <iostream> #include <string> #include <map> using namespace std; int LetterToNum(char c){ int res; if(c < 'Q'){ res = (int(c)-65+6)/3; } else{ res = (int(c)-81+21)/3; } return res; } int main(int argv,char *argc){ int i; long counter; bool outflag = false; string temps; string outs(""); map<string, int> mapNum; map<string , int>::iterator iter; string::iterator it; cin>>counter; while(counter--){ cin>>temps; for(it = temps.begin();it < temps.end();it++){ if(*it >= 'A' && *it < = 'Z'){ outs += LetterToNum(*it) + '0'; } if(*it >= '0' && *it < = '9'){ outs += *it; } if(*it == '-'){ continue; } } mapNum[outs]++; outs=""; } for(iter = mapNum.begin(); iter != mapNum.end(); iter++) { if(iter->second > 1){ for(i=0;i<iter ->first.size();i++){ if(i == 3) cout< <'-'; cout<<iter->first[i]; } cout< <" "<<iter->second<<endl; outflag = true; } } if(!outflag) cout<<"No duplicates."<<endl; return 0; }


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值