题目描述
程序员输入程序出现差错时,可以采取以下的补救措施:敲错了一个键时,可以补敲一个退格符“#”,以表示前一个字符无效;发现当前一行有错,可以敲入一个退行符“@”,以表示“@”与前一个换行符之间的字符全部无效。如:在终端上输入了这样两行字符:
PRKJ##OGRAN#M LX;
VAR@CONST N:#=10;
则实际有效的是:
PROGRAM LX;
CONST N=10;
输入
输入一行字符,个数不超过100
输出
输出一行字符,表示实际有效字符
样例输入 Copy
sdfosif@for(ii#=1,#;i<.#=8;i+++#);
样例输出 Copy
for(i=1;i<=8;i++);
这个题目总的来说没什么难度,基本写一遍就能ac,所以就这样吧
#include<iostream>
#include<stack>
using namespace std;
stack<char> shi;
char a[1000];
int main()
{
string s;
cin >> s;
for (char ch : s)
{
if (ch == '@')
{
while (!shi.empty())
{
shi.pop();
}
continue;
}
else if (ch == '#')
{
if (!shi.empty())
{
shi.pop();
}
}
else
{
shi.push(ch);
}
}
int count = 0;
while (!shi.empty())
{
a[count] = shi.top();
count++;
shi.pop();
}
for (int i = count-1; i >= 0; i--)
{
cout << a[i];
}
cout << endl;
return 0;
}