华为OJ中级题-设计购物单

华为OJ中级题-设计购物单_第1张图片
华为OJ中级题-设计购物单_第2张图片

struct Item
{
    int price;
    int imp;
    int fj;
};
struct Item2
{
    int price=0;
    int imp=0;
    double index=0;
};
int cmp(Item2 a, Item2 b){
    if (a.index == b.index){
 return a.price < b.price;

    }else
 return a.index > b.index;
}
int zyd(Item it){
    int s;
    s = it.price*it.imp;
 return s;
}
void HWOJ(){
    int sum = 1000;
    int wps = 5;
    int sumPrice = 0;
    int key[6] = { 0 };

    Item it[6];
    Item2 it2[6];
    it[1].price = 800; it[1].imp = 2; it[1].fj = 0;
    it[2].price = 400; it[2].imp = 5; it[2].fj = 1;
    it[3].price = 300; it[3].imp = 5; it[3].fj = 1;
    it[4].price = 400; it[4].imp = 3; it[4].fj = 0;
    it[5].price = 500; it[5].imp = 2; it[5].fj = 0;

    for (int i = 1; i <=wps; ++i){
        key[i] += zyd(it[i]);
        it2[i].price += it[i].price;
        if (it[i].fj > 0){
            key[i] += zyd(it[it[i].fj]);
            it2[i].price += it[it[i].fj].price;
        }
        it2[i].imp = key[i];
        it2[i].index = (double)it2[i].imp / (double)it2[i].price;
    }

    sort(it2+1, it2 + 6, cmp);
    for (int i = 1; i <= wps; ++i){
        cout << it[i].price << " " << it[i].imp << " " << it[i].fj << endl;
    }

    for (int i = 1; i <= wps; ++i){
        if (sum -it2[i].price>0){
            sumPrice += it2[i].imp;
            sum -= it2[i].price;
        }
        else continue;
    }
    cout << sumPrice << endl;
}

你可能感兴趣的:(华为OJ)