鱼C论坛

 找回密码
 立即注册
查看: 473|回复: 1

为什么我写的不对

[复制链接]
发表于 2023-11-26 21:13:47 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include <bits/stdc++.h>
using namespace std;
//  一般求最小值可以考虑小根堆去写
int main()
{
    priority_queue<int,vector<int>,greater<int> >q;
    int n,x;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>x;
        q.push(x);

    }
    int res=0;
    if(!q.empty())
    {
        int a= q.top();q.pop();
        int b=q.top();q.pop();
        int sum=a+b;
        res+=sum;
        q.push(sum);

    }
    cout<<res<<endl;

}
题目链接:https://www.luogu.com.cn/problem/P1090
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-11-26 21:14:01 | 显示全部楼层
你的代码中存在几个问题导致编译不通过:

1. 首先,在`priority_queue`的定义中,你没有指定堆中元素的类型。可以修改为`priority_queue<int>`来指定元素类型为整数。

2. 在for循环中,判断条件应该是`i<=n`而不是`i>x`。

3. 在for循环中,你没有读取每个数字x的输入语句。可以通过 `cin >> x;` 来读取每个数字。

4. 在最后的输出语句中,你没有指定要输出的变量res的值。可以修改为 `cout << res;` 来输出结果。

下面是修改后的代码:
#include <iostream>#include <queue>using namespace std;int main() {    priority_queue<int> q;    int n;    cin >> n;    for (int i = 1; i <= n; i++) {        int x;        cin >> x;        q.push(x);    }    int res = 0;    while (!q.empty()) {        int a = q.top(); q.pop();        if (q.empty()) {            res += a;            break;        }        int b = q.top(); q.pop();        int sum = a + b;        res += sum;        q.push(sum);    }    cout << res;    return 0;}
希望以上回答能够帮到你!如果还有其他问题,请随时提问。

以上回复来自 -- ChatGPT(FishC官方接口),如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-25 21:04

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表