當前位置:成語大全網 - 新華字典 - 生日相同問題,C語言編程,用字典結構解決,求解啊速度~~~

生日相同問題,C語言編程,用字典結構解決,求解啊速度~~~

樓主要求用C,麻煩呢,如果用C++倒是方便多了,閑著沒事做,簡單寫了下,看下就好

#include <iostream>

#include <map>

#include <list>

#include <string>

using namespace std;

struct BirthInfo

{

int month;

int day;

BirthInfo()

{

month = 0;

day = 0;

}

bool operator == (const BirthInfo& rhs ) const

{

if ( month == rhs.month && day == rhs.day )

{

return true;

}

return false;

}

bool operator < (const BirthInfo& rhs ) const

{

if ( month < rhs.month )

{

return true;

}

else if ( month == rhs.month )

{

if ( day < rhs.day )

{

return true;

}

}

return false;

}

};

struct SameBirthInfo

{

int nCount;

list<string> StrNoList;

SameBirthInfo()

{

nCount = 0;

}

};

typedef map<BirthInfo , SameBirthInfo> Result;

int main()

{

int nStudentCount = 0;

string strNo = "";

BirthInfo BirInfo;

Result result;

SameBirthInfo sameBirthInfo;

cin >> nStudentCount;

while( nStudentCount -- > 0 )

{

cin >> strNo >> BirInfo.month >> BirInfo.day;

Result::iterator iter = result.find( BirInfo );

if ( iter == result.end() )

{

//找不到

pair<Result::iterator , bool> pInsRet = result.insert( Result::value_type(BirInfo,sameBirthInfo) );

if ( pInsRet.second )

{

pInsRet.first->second.nCount = 1; //記錄下此生日有壹人

pInsRet.first->second.StrNoList.push_back( strNo );? //記錄下此人學號

}

}

else

{

//找到

iter->second.nCount++;? //同壹天生日人數++

iter->second.StrNoList.push_back( strNo );? //保存下這個學生的學號

}

}

//輸出所有結果

Result::const_iterator cIter = result.begin();

while ( cIter != result.end() )

{

//先輸出生日

cout << cIter->first.month << " " << cIter->first.day;

//輸出所有學生學號

list<string>::const_iterator cStrIter = cIter->second.StrNoList.begin();

while ( cStrIter != cIter->second.StrNoList.end() )

{

cout << " " << cStrIter->c_str();

++cStrIter;

}

cout << endl;

++cIter;

}

return 0;

}