@songpfei
2016-03-28T20:25:25.000000Z
字数 3390
阅读 1730
OJ_算法
华为OJ题:
// huawei_test.cpp : 定义控制台应用程序的入口点。
//
#include <string>
#include <iostream>
#include<queue>
using namespace std;
typedef struct tagPatient
{
string id;
bool type;
int cash;
int security;
int treat_state;
}Patient;
Patient g_patient[6];
queue<int> g_patient_queue;
static vector<string> split(string str, const string &pattern)
{
string::size_type pos;
vector<string> result;
string str_temp;
str += pattern;
string::size_type size = str.size();
for (string::size_type i = 0; i < size; i++)
{
pos = str.find(pattern, i);
if (pos < size)
{
str_temp = str.substr(i, pos - i);
result.push_back(str_temp);
i = pos + pattern.size() - 1;
}
}
return result;
}
static int GetPatientID(string partID)
{
int pat_id=0;
for (int i = 0; i < 6; i++)
{
if (g_patient[i].id == partID)
{
pat_id = i;
break;
}
}
return pat_id;
}
static bool PayFee(int part_id, int fee_account)
{
//缴挂号费或处方费
if ((g_patient[part_id].type == 1) && (g_patient[part_id].security >= fee_account))
{
g_patient[part_id].security -= fee_account;
}
else
{
if (g_patient[part_id].cash >= fee_account)
g_patient[part_id].cash -= fee_account;
else
return false;
}
return true;
}
static int GetPatientQueueSerialNumber(int part_id)
{
queue<int> patient_queue_temp(g_patient_queue);
int serial_num = 1;
while (!patient_queue_temp.empty())
{
if (patient_queue_temp.front() == part_id)
break;
serial_num++;
patient_queue_temp.pop();
}
return serial_num;
}
void initialize()
{
g_patient[0] = { "pat01", 0, 100, 0, 0 };
g_patient[1] = { "pat02", 1, 100, 100, 0 };
g_patient[2] = { "pat03", 0, 100, 0, 0 };
g_patient[3] = { "pat04", 1, 100, 50, 0 };
g_patient[4] = { "pat05", 1, 10, 10, 0 };
g_patient[5] = { "pat06", 1, 20, 10, 0 };
while (g_patient_queue.size() > 0)
{
g_patient_queue.pop();
}
cout << "E000" << endl;
}
void PatientRegister(string partID)
{
if (g_patient_queue.size() >= 4)
{
cout << "E003" << endl;
return;
}
int part_id = GetPatientID(partID);
if (g_patient[part_id].treat_state != 0)
{
cout << "E002" << endl;
return;
}
if (!PayFee(part_id, 10))
return;
g_patient_queue.push(part_id);
g_patient[part_id].treat_state = 1;
cout << "E001" << endl;
}
void PatientDiagnose()
{
if (g_patient_queue.empty())
{
cout << "E006" << endl;
return;
}
int part_id = g_patient_queue.front();
g_patient[part_id].treat_state = 2;
g_patient_queue.pop();
cout << "E005" << endl;
}
void PatientPay(string partID)
{
int part_id = GetPatientID(partID);
if (g_patient[part_id].treat_state != 2)
{
cout << "E014" << endl;
return;
}
if (!PayFee(part_id, 50))
cout << "E008" << endl;
else
cout << "E007" << endl;
g_patient[part_id].treat_state = 0;
}
void Query(string query_cmd)
{
if (query_cmd[0] == '0')
{
if (g_patient_queue.empty())
{
cout << "E013:dct 0" << endl;
return;
}
cout << "E013:dct";
queue<int> patient_queue_temp(g_patient_queue);
while (!patient_queue_temp.empty())
{
cout << " " << g_patient[patient_queue_temp.front()].id;
patient_queue_temp.pop();
}
cout << endl;
}
else
{
int part_id = GetPatientID(query_cmd.substr(2));
switch (g_patient[part_id].treat_state)
{
case 0:
cout << "E012:" << g_patient[part_id].id << " 0 ";
break;
case 1:
cout << "E012:" << g_patient[part_id].id << " 1 " << GetPatientQueueSerialNumber(part_id) << ",";
break;
case 2:
cout << "E012:" << g_patient[part_id].id << " 2 ";
break;
default:
break;
}
cout << g_patient[part_id].security << "," << g_patient[part_id].cash << endl;
}
}
int main()
{
string command_in;
vector<string> sub_cmd;
getline(cin, command_in);
sub_cmd = split(command_in, ",");
for (vector<string>::size_type i = 0; i < sub_cmd.size(); i++)
{
vector<string> split_cmd = split(sub_cmd[i], "_");
if (split_cmd[0] == "i")
initialize();
else if (split_cmd[0] == "reg")
{
PatientRegister(split_cmd[1]);
}
else if (split_cmd[0] == "diag")
{
PatientDiagnose();
}
else if (split_cmd[0] == "pay")
{
PatientPay(split_cmd[1]);
}
else if (split_cmd[0] == "qu")
{
Query(split_cmd[1]);
}
else
break;
}
return 0;
}