今天把POJ 1002给搞定了,本来想着练练排序算法呢,可中间用到了map容器,简直太完美了,就不用排序了,代码如下。# include <iostream> # include <string> # include <vector> # include <map> using namespace std; int main() { int N(0); cin >> N; vector<string> ivec; /*先把原始数据写到容器中*/ for (int i = 0; i != N; i++) { string str; cin >> str; ivec.push_back(str); } /*再对数据进行处理,使字母和数字对应,并去掉-符号*/ for (int i = 0; i != N; i++) { string strtemp = ivec[i]; string newstr; for ( int j=0; j != strtemp.length(); j++ ) { if ( strtemp[j] == 65 || strtemp[j] == 66 || strtemp[j] == 67 ) { newstr += '2'; } else if (strtemp[j] == 68 || strtemp[j] == 69 || strtemp[j] == 70) { newstr += '3'; } else if (strtemp[j] == 71 || strtemp[j] == 72 || strtemp[j] == 73) { newstr += '4'; } else if (strtemp[j] == 74 || strtemp[j] == 75 || strtemp[j] == 76) { newstr += '5'; } else if (strtemp[j] == 77 || strtemp[j] == 78 || strtemp[j] == 79) { newstr += '6'; } else if (strtemp[j] == 80 || strtemp[j] == 82 || strtemp[j] == 83) { newstr += '7'; } else if (strtemp[j] == 84 || strtemp[j] == 85 || strtemp[j] == 86) { newstr += '8'; } else if (strtemp[j] == 87 || strtemp[j] == 88 || strtemp[j] == 89) { newstr += '9'; } else if ( strtemp[j] == 45) { newstr = newstr; } else { newstr += strtemp[j]; } } newstr.insert(3,1,'-'); ivec[i] = newstr; /*现在所有的数都已经处理完毕存储为int,在ivec_int中*/ } map<string,int> imap; for (vector<string>::iterator iter = ivec.begin();iter != ivec.end();iter++) { ++imap[*iter]; } bool cout_flag(false); for (map<string,int>::iterator iter = imap.begin();iter != imap.end();iter++) { if ( (iter->second) > 1) { cout << iter->first << " " << iter->second << endl; cout_flag = true; } } if ( !cout_flag ) { cout << "No duplicates."; } }