3126. 商品推荐

10175102262 LarsPendragon

本来想节省一下代码,就在对价格排序之后直接从中间开始筛销量大于中位数的……然后WA,然后发现万一价格等于中位数的有很多呢……果然代码还是省不下去……
附C代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct {char name[10], s[20], p[100]; int no; double sale, price;}Goods;//为保证输出等于输入,用字符串存
int cmp(const void*a, const void*b)//总排序
{
    Goods *p1=(Goods*)a, *p2=(Goods*)b;
    if(p1->sale==p2->sale)
    {
        if(p1->price==p2->price) return p1->no-p2->no;
        return p1->price>p2->price?1:-1;
    }
    return p2->sale>p1->sale?1:-1;
}
int cmprice(const void*a, const void*b)//对价格排序
{
    Goods *p1=(Goods*)a, *p2=(Goods*)b;
    return p2->price>p1->price?1:-1;
}
int cmsales(const void*a, const void*b)//对销量排序
{
    Goods *p1=(Goods*)a, *p2=(Goods*)b;
    return p1->sale>p2->sale?1:-1;
}
int main()
{
    int T, I;
    scanf("%d",&T);
    for(I=0; I<T; I++)
    {
        Goods goods[100];
        int n, i, cnt=0;
        double sal, pri;
        scanf("%d",&n);
        for(i=0; i<n; i++)
        {
            scanf("%s %s %s",goods[i].name,goods[i].s,goods[i].p); 
            goods[i].no=i; 
            goods[i].price=atof(goods[i].p);
            goods[i].sale=atof(goods[i].s);
        }
        qsort(goods, n, sizeof(goods[0]), cmsales);//求销量中位数
        if(n%2) sal=goods[n/2].sale;
        else sal=(goods[n/2].sale+goods[n/2-1].sale)/2;
        qsort(goods, n, sizeof(goods[0]), cmprice);//求价格中位数
        if(n%2) pri=goods[n/2].price;
        else pri=(goods[n/2].price+goods[n/2-1].price)/2;
        for(i=0; i<n; i++) if(goods[i].sale>sal && goods[i].price<pri) goods[cnt++]=goods[i];//筛符合条件的商品
        qsort(goods, cnt, sizeof(goods[0]), cmp);
        printf("case #%d:\n",I);
        if(!cnt) printf("no recommendation\n");
        else for(i=0; i<cnt; i++) printf("%s %s %s\n",goods[i].name, goods[i].s, goods[i].p);
    }
    return 0;
}
Li Dao

我来吐槽两句(附AC代码)

include

using namespace std;
int T,n;
struct good
{
string ID;
int sale;
double price;
string price_str;
};

vector V;

int cmp_sale(const good& aa,const good& bb)
{
return aa.sale<bb.sale;
}
int cmp_price(const good& aa,const good& bb)
{
return aa.price-bb.price<0;
}
int cmp(const good& aa,const good& bb)
{
if(aa.sale!=bb.sale) return aa.sale>bb.sale;
else if(abs(aa.price-bb.price)>=1e-5) return aa.price-bb.price<0;
else return aa.ID<bb.ID;
}
void solve()
{
int n;
V.clear();

cin>>n;
for(int i=1;i<=n;i++)
{
string a;
int b;
string c0;
double c;
cin>>a>>b>>c0;
stringstream ss(c0);
ss>>c;
V.push_back((good){a,b,c,c0});
}

double mid_price;
int mid_sale;
sort(V.begin(),V.end(),cmp_price);
mid_price=V.size()%2==0?(V[V.size()/2].price+V[V.size()/2-1].price)/2:V[V.size()/2].price;

sort(V.begin(),V.end(),cmp_sale);
mid_sale=V.size()%2==0?(V[V.size()/2].sale+V[V.size()/2-1].sale)/2:V[V.size()/2].sale;

int flag=0;
sort(V.begin(),V.end(),cmp);
for(int i=0;imid_sale && V[i].price-mid_price<0)
{
flag=1;
printf(“%s %d %s\n”,V[i].ID.c_str(),V[i].sale,V[i].price_str.c_str());
}

if(!flag) cout<<”no recommendation”<<endl;
return;
}

int main()
{
scanf(“%d”,&T);
for(int step=0;step<T;step++)
{
printf(“case #%d:\n”,step);
solve();
}
return 0;
}

这题最有毒的是:竟然输出浮点数要和输入的格式一样。
6666
我怎么知道输入的时候保留几位小数
只好把string保存下来

aiden
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

struct List
{
    string No;
    unsigned sells;
    float price;
};

bool operator<(const List &lhs, const List &rhs)
{
    if (lhs.sells == rhs.sells)
        return lhs.price < rhs.price;
    return lhs.sells > rhs.sells;
}

void getSellsBar(const vector<List> &sorted_vec, unsigned &sells_bar)
{
    int size = sorted_vec.size();
    if (size % 2 == 1)
    {
        --size;
        sells_bar = sorted_vec[(size + 1) / 2].sells;
    }
    else
    {
        --size;
        sells_bar = (sorted_vec[size / 2].sells + sorted_vec[size / 2 + 1].sells) / 2;
    }
}

void getPriceBar(const vector<List> &sorted_vec, float &price_bar)
{
    int size = sorted_vec.size();
    if (size % 2 == 1)
    {
        --size;
        price_bar = sorted_vec[(size + 1) / 2].price;
    }
    else
    {
        --size;
        price_bar = (sorted_vec[size / 2].price + sorted_vec[size / 2 + 1].price) / 2;
    }
}

int main()
{
    int t;
    cin >> t;
    for (int z = 0; z < t; ++z)
    {
        int n;
        cin >> n;
        vector<List> vec;
        while (n--)
        {
            List temp;
            cin >> temp.No >> temp.sells >> temp.price;
            vec.push_back(temp);
        }
        unsigned sells_bar;
        sort(vec.begin(), vec.end(), 
            [ ](const List &lhs, const List &rhs) {return lhs.sells > rhs.sells; });
        getSellsBar(vec, sells_bar);    
        float price_bar;
        sort(vec.begin(), vec.end(),
            [ ](const List &lhs, const List &rhs) {return lhs.price < rhs.price; });
        getPriceBar(vec, price_bar);
        bool noRecommend = true;
        sort(vec.begin(), vec.end());
        cout << "case #" << z << ':' << endl;
        for (const auto &i : vec)
        {
            if (i.sells > sells_bar && i.price < price_bar)
            {
                noRecommend = false;
                cout << i.No << ' ' << i.sells << ' ' << i.price << endl;
            }
        }
        if (noRecommend)
            cout << "no recommendation" << endl;
    }
    return 0;
}
你当前正在回复 博客/题目
存在问题!