西工大附中初2014...吧 关注:18贴子:893
  • 3回复贴,共1

话说这是一个求生日概率那道题的一个程序,瞅一瞅(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;
}
说几句无关的,我觉得写得有点问题,张亳豪你瞅一瞅,哪的问题


IP属地:陕西1楼2013-10-19 20:09回复
    我家现在分不清粗体与细体了,估计是那的错


    IP属地:陕西2楼2013-10-19 20:12
    回复
      求你先看看吧规吧


      IP属地:北京来自手机贴吧3楼2013-10-20 07:39
      回复
        C++啊 好多地方不太懂呢 再加上小手机屏太小


        IP属地:北京来自手机贴吧4楼2013-10-20 07:41
        回复