uq_21409196860


私信TA

用户名:uq_21409196860

访问量:1101

签 名:

等  级
排  名 23234
经  验 595
参赛次数 0
文章发表 3
年  龄 0
在职情况 学生
学  校
专  业

  自我简介:

解题思路:
先输入,再排序,最后用贪心算法得到局部最优,从而推出全局最优
注意事项:
编译错误的比较多,运行错误的原因一般都是在遍历的时候忽略了一种边界,当货物全部装完,车还没装满的情况
参考代码:

#include<iostream>

#include<algorithm>

#include <stdio.h>

#include<vector>


using namespace std;



int main()

{

    vector< vector<float>list;

    vector<float> path;

    float number,max_weigh;

    cin>>number>>max_weigh;

    float num1,num2,nowprice=0;

    for(int i=0;i<number;i++)

    {

        cin>>num1>>num2;

        path.push_back(num2/num1);

        path.push_back(num1);

        path.push_back(num2);

        list.push_back(path);

        path.clear();

    }

    sort(list.begin(),list.end());


    while(max_weigh>0&&(!list.empty()))

    {

        path = list.back();

        if(max_weigh>path[1])

        {

            max_weigh -= path[1];

            nowprice += path[2];


        }

        else

        {

            nowprice += path[0]*max_weigh;

            max_weigh = 0;

        }

        path.clear();

        list.pop_back();

    }



    printf("%.1f",nowprice);



}


 

0.0分

1 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区