C++手写栈50pts求助
  • 板块P2104 二进制
  • 楼主Gold14526神金
  • 当前回复0
  • 已保存回复0
  • 发布时间2022/4/11 18:36
  • 上次更新2023/10/28 03:58:42
查看原帖
C++手写栈50pts求助
345930
Gold14526神金楼主2022/4/11 18:36
#include<bits/stdc++.h>
using namespace std;
struct stck{
	int a[1000001];
	int t;
	void clear(){t=0;}
	void push(int x){a[++t]=x;}
	void pop(){--t;}
	int top(){return a[t];}
	bool empty(){return !t;}
	int size(){return t;}
}s;
int c[10000001];
int main()
{
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	int m,n,k,x;
	scanf("%d%d",&n,&k);
	char ch=getchar();
	while(ch!='0'&&ch!='1')
	{
		ch=getchar();
	}
	while(ch=='0'||ch=='1')
	{
		s.push(ch-'0');
		ch=getchar(); 
	}
	while(ch!='+'&&ch!='-'&&ch!='*'&&ch!='/')ch=getchar();
	while(ch=='+'||ch=='-'||ch=='*'||ch=='/')
	{
		if(ch=='+')
		{
			x=s.top();
			s.pop();
			s.push(x+1);
		}
		else if(ch=='-')
		{
			x=s.top();
			s.pop();
			s.push(x-1);
		}
		else if(ch=='*')
		{
			s.push(0);
		}
		else if(ch=='/')
		{
			x=s.top();
			s.pop();
			x=(x>>1)+s.top();
			s.pop();
			s.push(x);
		}
		ch=getchar();
	}
	m=s.size();
	for(int i=1;i<=m;++i)
	{
		c[i]=s.top();
		s.pop();
	}
	for(int i=1;i<m;++i)
	{
		c[i+1]+=c[i]>>1;
		c[i]-=c[i]>>1<<1;
	}
	while(c[m]==0&&m>1)--m;
	for(int i=m;i>=1;--i)
	{
		putchar('0'+c[i]);
	}
	return 0;
}
2022/4/11 18:36
加载中...