本帖最后由 qq1242009750 于 2018-4-11 21:32 编辑 #include "stdafx.h"
#include <iostream>
#include <fstream>
using namespace std;
#define IN_FILENAME "hotel.in"
#define OUT_FILENAME "hotel.out"
struct Hotel
{
int style;
int expenditure;
};
class CInn
{
public:
CInn()
{
ifstream ifs;
ifs.open(IN_FILENAME);
if (!ifs)
{
cout << "文件打开失败!" << endl;
}
ifs >> m_num >> m_style >> m_expenditure;
m_Array = new Hotel[m_num];
for (int i = 0; ; ++i)
{
ifs >> m_Array[i].style;
ifs >> m_Array[i].expenditure;
if (ifs.eof())
break;
}
ifs.close();
}
void CalcNum() //计算符合条件的总和并打印文件
{
ofstream ofs;
ofs.open(OUT_FILENAME);
int Sum = 0;
for (int i = 0; i < m_style; ++i)
{
Sum += Sort(i);
}
ofs << Sum << endl;
}
~CInn()
{
if (m_Array)
delete[] m_Array;
}
private:
int m_num, m_style, m_expenditure;
Hotel *m_Array;
int Sort(int StyleNum)
{
int Sum = 0;
for (int i = 0; i < m_num; ++i) //计算某个风格客栈的总和
{
if (m_Array[i].style == StyleNum )
++Sum;
}
Hotel *StyleArray = new Hotel[Sum];
for (int i = 0, j = 0; i < m_num; ++i) //保存符合风格的客栈
{
if (m_Array[i].style == StyleNum)
{
StyleArray[j] = m_Array[i];
++j;
}
}
int HSum = 0;
for (int i = 0; i < Sum - 1; ++i) //计算可能性
{
for (int j = i + 1; j < Sum; ++j)
{
if (StyleArray[i].expenditure <= m_expenditure || StyleArray[j].expenditure <= m_expenditure) //计算符合花费的客栈
++HSum;
}
}
delete []StyleArray;
return HSum;
}
};
int main(void)
{
CInn a;
a.CalcNum();
system("pause");
return 0;
}
|