就是简单的字符串比较和排序。

   一开始看到之前写的代码是用了map,觉得没必要用map,只需要pair就可以了。等做完才发现原来输出要求是要按id的字典序排序的,而map在插入的过程中就自动排序了。所以最后还是用了map,省得自己写排序^_^。

   先贴下之前的代码,应该是不熟悉string(可能是混淆了C和C++的string导致错误又懒得去解决,所以使用了数字的比较,写得很繁琐,但很可爱地处理了输入"."的问题。←_←NC)

   需要注意的是每次循环需要先清空idMap。

#include <iostream>
#include <string>
#include <map>
using namespace std;
int main()
{  
    int n;
    string id[20];
    map<string, string> idMap;
    map<string, string>::iterator it;
    int ip[20][5];
    int i, j, k, m;
    char aDot;
    int count;
    cin >> n;
    while( n != 0 ) {
        idMap.clear();
        for( i = 0; i < n; i++ )
        {
            j = 0;
            cin >> id[i];
            for( j = 0; j < 3; j++ )
            {
                cin >> ip[i][j];
                cin >> aDot;
            }
            cin >> ip[i][j];
            ip[i][4] = 0; // 标志位,0为未发现
            for( k = 0; k < i; ++k )
            {
                if( ip[i][4] == 0 )
                {
                    count = 0;
                    for( m = 0; m < 4; ++m )
                    {
                        if( ip[k][m] != ip[i][m] )
                            break;
                        else
                            count++;
                    }
                    if ( count == 4 )
                    {
                        ip[i][4] = 1;
                        ip[k][4] = 1;
                        idMap.insert(pair<string, string>(id[k], id[i]));
                        break;
                    }
                }
            }
        }
        for( it = idMap.begin(); it != idMap.end(); ++it )
            cout << it -> second << " is the MaJia of " << it->first << endl;
        cout << endl;
        cin >> n;
    }
    return 0;
}


   接下来贴新写的代码。由于混淆了C和C++的string。include进来的是string却用了strcmp函数,导致报错——“no matching function for call to 'strcmp(std::string&, std::string&)”,查了一下发现C++直接用大于号小于号等号比较字符串,所以果断用C++的字符串了,而不改成include<cstring>。

#include <iostream>
#include <string>
#include <map>
using namespace std;
int main()
{  
    int n;
    string id[20], ip[20];
    map<string, string> idMap;
    map<string, string>::iterator it;
    int i, j, k, cnt;
    cin >> n;
    while( n != 0 ) {
        idMap.clear();
        for( i = 0; i < n; i++) {
            cin >> id[i] >> ip[i];
        }
        for (i=0;i<n;i++) {
            for (j=i+1;j<n;j++)
                if (ip[i] == ip[j])
                    idMap.insert(make_pair(id[i],id[j]));
        }
        for ( it = idMap.begin(); it != idMap.end(); ++it ) {
            cout << it->second << " is the MaJia of " << it->first << endl;
        }
        cout << endl;
        cin >> n;
    }
    return 0;
}