徐老师的机器校验


题目要求实际上就是要我们维护一个栈,可以直接用STL的栈做。如果要记录,就将该元素push进栈里面就好了,如果要输出,就输出堆顶元素并弹出就好了。
注意!这里必须要用scanf和printf!因为读入的数据个数为10^6,用cin,cout会超时!!!
超时(50分)代码:
#include <bits/stdc++.h>
using namespace std;
char a;
int n,m;
stack<int> s;
int main()
{
  //freopen("machine.in","r",stdin);
  //freopen("machine.out","w",stdout);
  cin>>n;
  for(int i = 0; i < n; i++)
  {
    cin>>a;
    if(a == 'P')
    {
      cout<<s.top()<<endl;
      s.pop();
    }
    else
    {
      cin>>m;
      s.push(m);
    }
  }
  return 0;
}

正解:

#include <bits/stdc++.h>
using namespace std;
char a;
int n,m;
stack<int> s;
int main()
{
  //freopen("machine.in","r",stdin);
  //freopen("machine.out","w",stdout);
  scanf("%d",&n);
  for(int i = 0; i < n; i++)
  {
    scanf("%c",&a);//因为会把换行符\n也当成一个字符,所以得多输入一次(接受\n) 
    scanf("%c",&a);
    if(a == 'P')
    {
      printf("%d\n",s.top());
      s.pop();
    }
    else
    {
      scanf("%d",&m);
      s.push(m);
    }
  }
  return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值