#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <stdio.h>
#include "math.h"
using namespace std;
struct food
{
string name;
int price;
bool isHun;
bool isXinla;
};
static int nFoodMenu; //菜单上菜的数目
static int nFoodNeed; //饭团需要点的菜的数目
static int nPeople; //就餐的人数
//荤菜,素菜,辛辣,清淡菜
static int nHunFood;
static int nSuFood;
static int nXinFood;
static int nQindanFood;
vector<food> menu;
vector< vector<food> > chosenMenus;
vector<food> bestMenu;
double Distance15yuan(const vector<food>& chosenMenu)
{ //所选菜与15元相差多少
// test price
int totalPrice = 0;
for(vector<food>::const_iterator iter = chosenMenu.begin(); iter != chosenMenu.end(); ++iter)
{
totalPrice += (*iter).price;
}
return abs(totalPrice/nPeople - 15); //8折
}
void GetBestMenu()
{
double priceDistance = 1e+308;
double thisDistance = 0.0;
for(vector< vector<food> >::iterator iter = chosenMenus.begin(); iter != chosenMenus.end(); ++iter)
{
thisDistance = Distance15yuan(*iter);
if(thisDistance < priceDistance)
{
priceDistance = thisDistance;
bestMenu = *iter;
}
}
}
bool TestCondition(vector<food> chosenFood)
{ //是否符合荤菜,素菜,辛辣,清淡菜数量要求
int nTestHunFood = 0;
int nTestSuFood = 0;
int nTestXinFood = 0;
int nTestQindanFood = 0;
for(vector<food>::iterator iter = chosenFood.begin(); iter != chosenFood.end(); ++iter)
{
if((*iter).isHun == true)
{
++nTestHunFood;
}
else
{
++nTestSuFood;
}
}
if(nTestHunFood != nHunFood)
{
return false;
}
if(nTestSuFood != nSuFood)
{
return false;
}
for(vector<food>::iterator iter = chosenFood.begin(); iter != chosenFood.end(); ++iter)
{
if((*iter).isXinla == true)
{
++nTestXinFood;
}
else
{
++nTestQindanFood;
}
}
if(nTestXinFood != nXinFood)
{
return false;
}
if(nTestQindanFood != nQindanFood)
{
return false;
}
return true;
}
void CreateChosenMenus(vector<food>leftFood, vector<food> rightFood)
{
if(leftFood.size() == nFoodNeed)
{
if(TestCondition(leftFood) == true)
{
chosenMenus.push_back(leftFood);
}
return;
}
for(vector<food>::iterator iter = rightFood.begin(); iter != rightFood.end(); ++iter)
{
vector<food> newLeftFood(leftFood);
newLeftFood.push_back(*iter);
vector<food> newRightFood;
for(vector<food>::iterator i = iter+1; i != rightFood.end(); ++i)
{
newRightFood.push_back(*i);
}
CreateChosenMenus(newLeftFood, newRightFood);
}
}
void TryToSolove()
{
vector<food> left;
CreateChosenMenus(left, menu);
GetBestMenu();
}
int main(int argc, char* argv[])
{
ifstream is(argv[1], ios::in | ios::binary);
is >> nFoodMenu;
is >> nFoodNeed;
is >> nPeople;
food temp;
for(int i = 0; i < nFoodMenu; ++i)
{
is >> temp.name;
is >> temp.price;
is >> temp.isHun ;
is >> temp.isXinla;
menu.push_back(temp);
}
is >> nHunFood;
is >> nSuFood;
is >> nXinFood;
is >> nQindanFood;
TryToSolove();
double averagePrice = 0.0;
for(vector<food>::iterator iter = bestMenu.begin(); iter != bestMenu.end(); ++iter)
{
cout << (*iter).name << endl;
averagePrice += (*iter).price;
}
printf("%0.2f\n", averagePrice/nPeople*0.8);
return 0;
}
SeedCoder2014热身题9:解答
最新推荐文章于 2019-08-23 10:34:25 发布