NEFU 大一寒假训练七(栈)2020.01.06

Summary

寒假训练的最后一天,祝大家下午考试和以后的练习中多多 AC
Accepted
Wrong Answer Runtime Error Time Limit Exceeded
Output Limit Exceeded Compile Error Memory Limit Exceeded
Presentation Error Floating Point Error

Information

Problem Title
1624 栈-程序员输入问题
1627 栈-溶液模拟器
1628 栈-火车编组
1629 栈-洗盘子
1630 栈-括号匹配
1631 栈-表达式求值

Problem 1624: 栈-程序员输入问题

Tips

用数组模拟栈更方便,省去了倒序的麻烦。

Code

#include <bits/stdc++.h>
using namespace std;

int main()
{
	char tmp,sta[101],sp=0;
	while(scanf("%c",&tmp)!=-1)
	{
		if(tmp=='#')sp--;
		else if(tmp=='@')sp=0;
		else sta[sp++]=tmp; 
	}
	for(int i=0;i<sp;i++)
	{
		printf("%c",sta[i]);
	}
	return 0;
}

Problem 1627: 栈-溶液模拟器

Tips

记住化学公式就没问题了 cv = c1v1 * c2v2

Code

#include <bits/stdc++.h>
using namespace std;

int main()
{
	double aqc[10000],c;
	int cp=1,n,aqv[10000],v;
	char o;
	scanf("%d %lf",&aqv[0],&aqc[0]);
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	{
		scanf(" %c",&o);
		if(o=='P')
		{
			scanf("%d %lf",&v,&c);
			aqv[cp]=aqv[cp-1]+v;
			aqc[cp]=(aqc[cp-1]*aqv[cp-1]+c*v)/aqv[cp];
			cp++;
		}
		else if(cp>1)cp--;
		printf("%d %.5f\n",aqv[cp-1],aqc[cp-1]);
	}
	return 0;
}

Problem 1628: 栈-火车编组

Tips

我是一只小蒟蒻,栈是什么?没吃过。我就不用,快来打我啊 ~ ヾ(•ω•`)o
这个题感觉难度还是比较大的,简单分享一下思路

  • 编组后的第一节车厢先出栈,说明前面的车厢一定提前入好栈了
  • 对于后面每一节车厢,如果上一节车厢编号比它大,它就可以直接出栈
  • 如果后面的车厢 tmp 比以前所有的车厢编号都要大(也就是大于最大编号的车厢 max),那么需要先入栈 max-tmp 次才能出栈

Code

#include <bits/stdc++.h>
using namespace std;

int main()
{
	ios::sync_with_stdio(false);
	int n,base,tmp,lb; //base上一节车厢编号 tmp本节车厢编号 lb前面最大的车厢编号
	cin>>n;
	for(int i=0;i<n;i++)
	{
		cin>>tmp;
		if(i) //判断是否为第一节车厢
		{
			if(tmp<base)cout<<"B"; //比上一节车厢编号小直接出栈
			else
			{
				for(int j=0;j<tmp-lb;j++) //比上一节车厢编号大先进栈tmp-lb次
				{
					cout<<"A";
				}
				cout<<"B"; //自己出栈
			} 
		}
		else
		{
			lb=base=tmp; //设置上一节车厢编号和最大的车厢编号
			for(int j=0;j<base;j++)cout<<"A"; //先把这节车厢前面所有的车厢进栈
			cout<<"B"; //自己出栈
		}
		base=tmp;
		if(tmp>lb)lb=tmp;
	}
	return 0;
}

Problem 1629: 栈-洗盘子

Tips

用 3 个栈,分别存放脏盘子,洗完的盘子,擦完的盘子

Code

#include <bits/stdc++.h>
using namespace std;

stack <int> dirty,wash,clean;
int main()
{
	ios::sync_with_stdio(false);
	int n,o,num;
	cin>>n;
	for(int i=n;i>=1;i--)
	{
		dirty.push(i);
	}
	while(cin>>o>>num)
	{
		if(o==1)
		{
			while(num--)
			{
				wash.push(dirty.top());
				dirty.pop();
			}
		}
		else
		{
			while(num--)
			{
				clean.push(wash.top());
				wash.pop();
			}
		}
	}
	while(n--)
	{
		cout<<clean.top()<<endl;
		clean.pop();
	} 
	return 0;
}

Problem 1630: 栈-括号匹配

Tips

判断 sta.top() 之前一定要判断 sta.empty() 要不然会 RE

Code

#include <bits/stdc++.h>
using namespace std;

stack <char> sta;
int main()
{
	ios::sync_with_stdio(false);
	char tmp,ok=1;
	while(cin>>tmp)
	{
		if(tmp=='('||tmp=='[')sta.push(tmp);
		else if(!sta.empty()&&tmp==')'&&sta.top()=='(')sta.pop();
		else if(!sta.empty()&&tmp==']'&&sta.top()=='[')sta.pop();
		else
		{
			ok=0;
			break;
		}
	}
	if(ok&&sta.empty())cout<<"OK";
	else cout<<"Wrong";
	return 0;
}

Problem 1631: 栈-表达式求值

Tips

优先计算乘法,把计算好的结果存到栈里,最后相加
没事就取一下模,防止爆 int

Code

#include <bits/stdc++.h>
using namespace std;

stack <int> sta;
int main()
{
	ios::sync_with_stdio(false);
	char tmp,op=0;
	int num=0,ans=0;
	while(cin>>tmp)
	{
		if(tmp>='0'&&tmp>=9)num=(num*10+tmp-'0')%10000;
		else if(tmp=='+')
		{
			if(op=='*')
			{
				num*=sta.top();
				num%=10000;
				sta.pop();
			}
			sta.push(num);
			num=0;
			op=tmp;
		}
		else if(tmp=='*')
		{
			if(op=='*')
			{
				num*=sta.top();
				num%=10000;
				sta.pop();
			}
			sta.push(num);
			num=0;
			op=tmp;
		}
	}
	if(op=='*')
	{
		num*=sta.top();
		num%=10000;
		sta.pop();
	}
	sta.push(num);
	while(!sta.empty())
	{
		ans+=sta.top();
		ans%=10000;
		sta.pop();
	}
	cout<<ans;
	return 0;
}
发布了32 篇原创文章 · 获赞 104 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/csg999/article/details/103852196