您的位置:首页 > 其它

堆栈 九度1108 1101

2018-01-25 17:42 225 查看
题目描述:1108
    堆栈是一种基本的数据结构。堆栈具有两种基本操作方式,push 和 pop。Push一个值会将其压入栈顶,而 pop 则会将栈顶的值弹出。现在我们就来验证一下堆栈的使用。

输入:
     对于每组测试数据,第一行是一个正整数 n,0<n<=10000(n=0 结束)。而后的 n 行,每行的第一个字符可能是'P’或者'O’或者'A’;如果是'P’,后面还会跟着一个整数,表示把这个数据压入堆栈;如果是'O’,表示将栈顶的值 pop 出来,如果堆栈中没有元素时,忽略本次操作;如果是'A’,表示询问当前栈顶的值,如果当时栈为空,则输出'E'。堆栈开始为空。

输出:
    对于每组测试数据,根据其中的命令字符来处理堆栈;并对所有的'A’操作,输出当时栈顶的值,每个占据一行,如果当时栈为空,则输出'E’。当每组测试数据完成后,输出一个空行。

样例输入:
3
A
P 5
A
4
P 3
P 6
O
A
0


样例输出:
E
5

3


#include <iostream>

#include<stdio.h>

#include<stack>

#include<stdlib.h> 

using namespace std; 

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

stack<int>s;

int main(int argc, char** argv) {
int n;
while(scanf("%d",&n)!=EOF)
{
if(n==0)break;
char tmp[20];
for(int i=0;i<n;i++)
{
gets(tmp);
for(int j=0;tmp[j]!='\0';j++)
{
if(tmp[j]=='A'&&!s.empty())printf("%d\n",s.top());
else if(tmp[j]=='A'&&s.empty())printf("E\n");
else if(tmp[j]=='P')
{
j+=2;
int t=tmp[j]-'0';
s.push(t);
}
else if(tmp[j]=='O'&&!s.empty())
{
j+=2;
s.pop();
}
}
}
}
return 0;

}

%s用着不行,发现里面遇到空格就终止了,最后我改成了gets才好。

1101

题目描述:
对于一个不存在括号的表达式进行计算

输入:
存在多种数据,每组数据一行,表达式不存在空格

输出:
输出结果

样例输入:
6/2+3+3*4


样例输出:
18


--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

用栈模拟计算表达式,一般情况下,根据数据结构上介绍要建立操作数与操作符两个栈,并且设立优先级,根据优先级进行计算。但此题中有说明:没有括号,表达式不含空格,只有加、减、乘、除,因此可以简化,不必设立优先级,在读取时,遇到乘除时,直接从栈中弹出数据计算,因为按顺序计算先碰到乘除符号优先级最高;如果遇到减号,可以将后面的数据看成是负数。因此,简化之后,当遍历结束只需要将栈中的数据相加即为结果。

哈哈哈,感觉我用了最复杂的方法做。。。

#include <iostream>

#include<stdio.h>

#include<stdlib.h>

#include<stack>

using namespace std;

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

stack<int>op;

stack<double>ans;

char str[111];//遇到操作符,优先级高则进栈,优先级低则换出高的,弹出操作数计算。 

int prior[5][5]=

{
1,0,0,0,0,
1,0,0,0,0,
1,0,0,0,0,
1,1,1,0,0,
1,1,1,0,0,

};

void getop(bool &reto,int &retn,int &i)

{
if(i==0&&op.empty())
{
reto=true;
retn=0;
return;
}
else if(str[i]=='\0')
{
reto=true;
retn=0;
return;
}
if(str[i]>='0'&&str[i]<='9')
{
reto=false;
}
else
{
reto=true;
if(str[i]=='+')
{
retn=1;
}
else if(str[i]=='-')
{
retn=2;
}
else if(str[i]=='*')
{
retn=3;
}
else if(str[i]=='/')
{
retn=4;
}
i+=1;
return;
}
retn=0;
for(;str[i]>='0'&&str[i]<='9';i++)
{
retn*=10;
retn+=str[i]-'0';
}
return;

}

int main(int argc, char** argv) {
while(gets(str))
{
while(!op.empty())op.pop();
while(!ans.empty())ans.pop();
bool retop;
int retnum;
int idk=0;
while(1)
{
getop(retop,retnum,idk);
if(retop==true)
{
if(op.empty()||prior[retnum][op.top()]==1)
{
op.push(retnum);
}
else
{
while(prior[retnum][op.top()]==0)
{
double optmp=op.top();
op.pop();
double b=ans.top();
ans.pop();
double a=ans.top();
ans.pop();
double tmp;
if(optmp==1)tmp=a+b;
else if(optmp==2)tmp=a-b;
else if(optmp==3)tmp=a*b;
else tmp=a/b;
ans.push(tmp);
}
op.push(retnum);
}
}
else
{
ans.push(retnum);
}
if(op.size()==2&&op.top()==0)
{
break;
}
}
printf("%.f\n",ans.top());

}
return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: