小 z 想找份实习 2020-04-24 17:19 采纳率: 0%
浏览 112

PAT A1057 STACK(30) 有没有大佬能帮我看看这道题哪里错了?只得了10分

图片说明

图片说明
图片说明

#include<cstdio>
#include<string>
#include<iostream>
#include<stack>
#include<set>
using namespace std;
int n;
int main()
{
    cin>>n;
    stack<int> s;
    multiset<int> order;
    int key;
    for(int i = 0; i < n; i++)
    {
        string ope;
        cin>>ope;
        if(ope == "Push")
        {
            cin>>key;
            s.push(key);
            order.insert(key);
        }
        else if(ope == "Pop" && !s.empty())
        {
            int val = s.top();
            cout<<val<<endl;
            s.pop();
            order.erase(val);
        }
        else if(ope == "PeekMedian")
        {
            if(order.empty())
            cout<<"Invalid"<<endl;
            else
            {
                int num = s.size();
                num = (num%2 == 0)? num/2 : (num+1)/2;
                auto it = order.begin();
                cout<<(*it+num-1)<<endl;
            }
        }
        else
        {
            cout<<"Invalid"<<endl;
        }
    }
    return 0;
 } 
  • 写回答

1条回答 默认 最新

  • 码农阿豪@新空间代码工作室 Java领域优质创作者 2024-07-25 12:51
    关注
    让阿豪来帮你解答,本回答参考chatgpt3.5编写提供,如果还有疑问可以评论或留言
    问题:请解释上述代码的功能,并且给出一个示例。 回答:上述代码实现了一个数据结构,支持Push、Pop和PeekMedian操作。Push操作将元素压入栈中,并在一个有序集合中记录元素顺序;Pop操作将栈顶元素弹出,并从有序集合中删除对应元素;PeekMedian操作返回栈中位数元素。如果栈为空或者操作无效,则输出"Invalid"。 示例: 输入: 7 Push 3 Push 2 Push 1 PeekMedian Pop PeekMedian Pop 输出: 2 2 1 代码示例:
    #include <cstdio>
    #include <string>
    #include <iostream>
    #include <stack>
    #include <set>
    using namespace std;
    int n;
    int main(){
        cin >> n;
        stack<int> s;
        multiset<int> order;
        int key;
        for(int i = 0; i < n; i++)
        {
            string ope;
            cin >> ope;
            if(ope == "Push")
            {
                cin >> key;
                s.push(key);
                order.insert(key);
            }
            else if(ope == "Pop" && !s.empty())
            {
                int val = s.top();
                cout << val << endl;
                s.pop();
                order.erase(val);
            }
            else if(ope == "PeekMedian")
            {
                if(order.empty())
                    cout << "Invalid" << endl;
                else
                {
                    int num = s.size();
                    num = (num % 2 == 0) ? num / 2 : (num + 1) / 2;
                    auto it = order.begin();
                    cout << (*it + num - 1) << endl;
                }
            }
            else
            {
                cout << "Invalid" << endl;
            }
        }
        return 0;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同