p1241 63分,哪位大佬帮忙看下哪里有问题
  • 板块学术版
  • 楼主Leon211
  • 当前回复2
  • 已保存回复2
  • 发布时间2020/5/14 00:11
  • 上次更新2023/11/7 02:30:36
查看原帖
p1241 63分,哪位大佬帮忙看下哪里有问题
335266
Leon211楼主2020/5/14 00:11

没用栈设置了一个tag值。该数配队成功置tag。 以下是代码。

int main()
{
	string str;
	cin >> str;
	int n = 0;
	int i = 0;
	int temp;
	int sym = 1;
	char a[200];
	bool tags[200] = { 0 };
	while (n < str.length())
	{
	 if(str[n] == ')'|| str[n] == ']')
	 {
		temp = i;
		a[++i] = str[n];
		while(temp > 0)
		{
		  if ((a[temp] == '(' && a[i] == ')' && tags[temp]==0) || (a[temp] == '[' && a[i] == ']' && tags[temp] == 0))
		  {
			tags[temp] = 1;
			tags[i] = 1;
			break;
		  }
		  else temp--;
		}
	}
	else
	{
	  a[++i] = str[n];
	 }
	 n++;
	}
	for (int j = 1; j <=n; j++)
	{
if (a[j]=='('&&tags[j]==0)cout<< a[j]<< ')';
else if (a[j]=='['&&tags[j]==0)cout<<a[j]<<']';
else if (a[j]==')'&&tags[j]==0)cout<< '('<<a[j];
else if (a[j]==']'&&tags[j]==0)cout<<'['<< a[j];
else cout<< a[j];
	}
	return 0;
}



2020/5/14 00:11
加载中...