前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >使用STL标准库解决之前三个问题

使用STL标准库解决之前三个问题

作者头像
废江_小江
发布2022-09-05 13:09:23
发布2022-09-05 13:09:23
18800
代码可运行
举报
文章被收录于专栏:总栏目总栏目
运行总次数:0
代码可运行

Question

Stack

Coding

代码语言:javascript
代码运行次数:0
运行
复制
#include<iostream>
#include<stack>
#include<cstdio>
#include<stdlib.h>
using namespace std;
stack<int> st;
int main(){
	char a[10];
	int e1,e2;
	while(scanf("%s",&a)!=EOF){
		if(a[0]=='+'){
			e1=st.top();st.pop();
			e2=st.top();st.pop();
			st.push(e1+e2);
		}
		else if(a[0]=='-'){
			e1=st.top();st.pop();
			e2=st.top();st.pop();
			st.push(e2-e1);
		}
		else if(a[0]=='*'){
			e1=st.top();st.pop();
			e2=st.top();st.pop();
			st.push(e1*e2);
		}
		else{
			st.push(atoi(a));
		}
	}
	printf("%d\n",st.top());
}

OtherCoding

使用c++中的string来写

代码语言:javascript
代码运行次数:0
运行
复制
#include<iostream>
#include<stack>
#include<cstdio>
#include<stdlib.h>
#include<string>
using namespace std;
stack<int> st;
int main(){
	string a;
	int e1,e2;
	while(cin>>a){
		if(a[0]=='+'){
			e1=st.top();st.pop();
			e2=st.top();st.pop();
			st.push(e1+e2);
		}
		else if(a[0]=='-'){
			e1=st.top();st.pop();
			e2=st.top();st.pop();
			st.push(e2-e1);
		}
		else if(a[0]=='*'){
			e1=st.top();st.pop();
			e2=st.top();st.pop();
			st.push(e1*e2);
		}
		else{
			st.push(atoi(a.c_str())); //string转为char*使用c_str ;char*直接赋值给string 
		}
	}
	cout<<st.top()<<endl;
}

Question2

Queue

Coding

代码语言:javascript
代码运行次数:0
运行
复制
#include<iostream>
#include<queue>
#include<cstdio>
#include<stdlib.h>
#include<string>
using namespace std;
typedef struct{
	string name;
	int time;
}Q;
int sum;
queue<Q> qu;
int main(){
	int n ,q;
	Q tmp;
	cin>>n>>q;
	for(int i=0;i<n;i++){
		cin>>tmp.name>>tmp.time;
		qu.push(tmp);
	}
	while(!qu.empty()){
		tmp=qu.front();
		qu.pop();
		if(tmp.time<=q){
			sum+=tmp.time;
			cout<<tmp.name<<" "<<sum<<endl;
		}
		else{
			tmp.time-=q;
			sum+=q;
			qu.push(tmp);
		}
	}
	return 0;
}

Question3

DoubleList

Coding

代码语言:javascript
代码运行次数:0
运行
复制
#include<iostream>
#include<list>
#include<cstdio>
#include<stdlib.h>
#include<string>
using namespace std;
list<int> ls;
int main(){
	int n,a;
	cin>>n;
	string name;
	for(int i=0;i<n;i++){
		cin>>name;
		if(name[0] == 'i'){
			cin>>a;
			ls.push_front(a);
		}
		else if(name[6]=='F'){
			ls.pop_front();
		}
		else if(name[6]=='L'){
			ls.pop_back();
		}
		else{
			cin>>a;
			for(list<int>::iterator it =ls.begin(); it !=ls.end();it++){
				if(*it == a){
					ls.erase(it);
					break;
				}
			}
		}	
	}
	int i=0;
	for(list<int>::iterator it =ls.begin(); it !=ls.end();it++){
		if(i++)cout<<" ";
		cout<<*it;
	}
	cout<<endl;
	return 0;
}

这个可以直接ac,应该之前过三个测试点的问题就出在自己写的双向链表代码

废江博客 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权

转载请注明原文链接:使用STL标准库解决之前三个问题

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-04-07),如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Question
    • Coding
    • OtherCoding
  • Question2
    • Coding
  • Question3
    • Coding
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档