注册 登录
编程论坛 C++教室

bool cmp()函数种结构体做参数为什么能够省略struct

troyzyc 发布于 2018-03-13 17:13, 1647 次点击
#include<iostream>
#include<algorithm>
using namespace std;
const int M=1000005;

struct three
{
    double w;
    double v;
    double p;
}s[M];

bool cmp(three a,three b)  //这里我试验了下,写成bool cmp(struct three a,struct three b)也可以。为什么能够省略struct?我认为struct three这是不能分开的呀,就是表示一种类型。
{
    return a.p>b.p;
}

int main()
{
    int n;
    double m;
    cin>>n>>m;
    for(int i=0;i<n;i++)
    {
        cin>>s[i].w>>s[i].v;
        s[i].p=s[i].v/s[i].w;
    }
    sort(s,s+n,cmp);
   
    double sum=0.0;   
    for(int i=0;i<n;i++)
    {
        if(m>s[i].w)
        {
            sum+=s[i].v;
            m-=s[i].w;
        }
        else
        {
            sum+=m*s[i].p;
            break;
        }
    }
    cout<<sum<<endl;
   
    return 0;
}
1 回复
#2
yangfrancis2018-03-13 22:35
C不行,C++可以
1