社区讨论

求助玄学问题

P1106删数问题参与者 2已保存回复 1

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
1 条
当前快照
1 份
快照标识符
@lo7jh2bp
此快照首次捕获于
2023/10/27 02:49
2 年前
此快照最后确认于
2023/10/27 02:49
2 年前
查看原帖
rt,遇到了玄学问题,求大佬指教
WA+RE+AC 代码:
CPP
#include<bits/stdc++.h>
using namespace std;
stack<char>s;
string st;int k;
int main()
{
	cin>>st>>k;
	for(int i=0;i<st.length();++i)
	{
		if(!k)
		{
			s.push(st[i]);
			continue;
		}
		if(s.empty())s.push(st[i]);
		else
		{
			if(s.top()>st[i])
			{
				s.pop();--k;
			}
			else s.push(st[i]);
		}
	}
	while(k>0){--k;s.pop();}
	string num("");
	while(s.size())
	{
		num=s.top()+num;
		s.pop();continue;
	}
	string res("");
	for(int i=0;i<num.length();++i)
		if(!res.length() and num[i]=='0')
			continue;else res+=num[i];
	if(res.empty())cout<<"0";
        else cout<<res;
}
AC 代码:
CPP
#include<bits/stdc++.h>
using namespace std;
stack<char>s;
string st;int k;
int main()
{
	cin>>st>>k;
	for(int i=0;i<st.length();)
	{
		if(!k)
		{
			s.push(st[i++]);
			continue;
		}
		if(s.empty())s.push(st[i++]);
		else
		{
			if(s.top()>st[i])
			{
				s.pop();--k;
			}
			else s.push(st[i++]);
		}
	}
	while(k>0){--k;s.pop();}
	string num("");
	while(s.size())
	{
		num=s.top()+num;
		s.pop();continue;
	}
	string res("");
	for(int i=0;i<num.length();)
		if(!res.length() and num[i]=='0')
			{++i;continue;}
		else res+=num[i++];
	if(res.empty())cout<<"0";
        else cout<<res;
}

回复

1 条回复,欢迎继续交流。

正在加载回复...