关闭

2006百度之星程序设计大赛预赛题目之饭团的烦恼 参考解答

标签: 百度iteratordistancevectoriostreammath.h
2350人阅读 评论(1) 收藏 举报
分类:

#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;
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:95306次
    • 积分:1238
    • 等级:
    • 排名:千里之外
    • 原创:23篇
    • 转载:3篇
    • 译文:0篇
    • 评论:59条
    文章分类
    最新评论
    交通查询