10分请问哪里错了?

B3758 [信息与未来 2021] 括号序列

CCA_zhujunwei @ 2023-10-15 15:08:54

10分求条

#include<iostream>
using namespace std;
#define ll long long
ll n, k, cc;
string s;
int main(){
    getline(cin, s);
    for(int i = 0;i < s.size() - 1;i++){
        if(s[i] == '('){
            ++n;
            cout<<s[i];
        }
        else if(s[i] == ')'){
            --n;
            cout<<s[i];
        }   
    }
    while(n > 0){
        n--;
        cout<<')';
    }
    while(n < 0){
        n++;
        cout<<'(';
    }
    return 0;
}

by naroto2022 @ 2023-10-15 15:11:33

你这样做会有括号交叉的情况:如:(())) 则输出(()))(


by naroto2022 @ 2023-10-15 15:13:42

而且在循环的时候,你为什么要把字符串的长度减一?


by CCA_zhujunwei @ 2023-10-15 15:16:17

哦哦哦,疏忽了,谢谢


by CCA_zhujunwei @ 2023-10-16 19:14:42

已过


|