如何解决这个问题?显示基思误差 我写的是一个简单的C++程序>/P> #include <bits/stdc++.h> #define ll long long #define ul unsigned long long #define ld long double #define rep(i, a, b) for (int i = (a); i < (b); i++) #define repi(i, a, b) for (int i = (a); i > (b); i--) #define all(x) x.begin(), x.end() #define ks(x) (cout << #x << ":" << (x) << '\n') #define fastio ios_base::sync_with_stdio(false), cin.tie(nullptr) #define gcd _gcd using namespace std; const ll mod = 1000000007; int main() { fastio; ll tc = 1; cin >> tc; for (ll t = 0; t < tc; t++) { ll n; cin >> n; string s; cin >> s; ll cnt = 0; ll i = n - 1; if (s[n - 1] == ')') { i--; cnt++; while (s[i] == ')' && i > -1) { i--; cnt++; } } if (cnt > n / 2) cout << "YES\n"; else { cout << "NO\n"; } } return 0; }

如何解决这个问题?显示基思误差 我写的是一个简单的C++程序>/P> #include <bits/stdc++.h> #define ll long long #define ul unsigned long long #define ld long double #define rep(i, a, b) for (int i = (a); i < (b); i++) #define repi(i, a, b) for (int i = (a); i > (b); i--) #define all(x) x.begin(), x.end() #define ks(x) (cout << #x << ":" << (x) << '\n') #define fastio ios_base::sync_with_stdio(false), cin.tie(nullptr) #define gcd _gcd using namespace std; const ll mod = 1000000007; int main() { fastio; ll tc = 1; cin >> tc; for (ll t = 0; t < tc; t++) { ll n; cin >> n; string s; cin >> s; ll cnt = 0; ll i = n - 1; if (s[n - 1] == ')') { i--; cnt++; while (s[i] == ')' && i > -1) { i--; cnt++; } } if (cnt > n / 2) cout << "YES\n"; else { cout << "NO\n"; } } return 0; },c++,c++11,mingw,C++,C++11,Mingw,但它显示了我无法理解的输出,请帮助 /home/keith/builds/mingw/gcc-9.2.0-mingw32-cross-native/mingw32/libstdc++-v3/include/bits/basic_string.h:1067: std::\uuuCXX11::基本字符串::引用 std::uuCXX11::基本字符串::运算符[](std::uuCXX11::基本字符串::大小类型)[带CharT=char;Traits=std::char\u Traits;All

但它显示了我无法理解的输出,请帮助

/home/keith/builds/mingw/gcc-9.2.0-mingw32-cross-native/mingw32/libstdc++-v3/include/bits/basic_string.h:1067: std::\uuuCXX11::基本字符串::引用 std::uuCXX11::基本字符串::运算符[](std::uuCXX11::基本字符串::大小类型)[带CharT=char;Traits=std::char\u Traits;Alloc=std::分配器; std::\uuuCXX11::基本字符串::引用= 字符和;标准::基本字符串::大小类型 =unsigned int]:断言“\uuu pos断言
\uuu pos size(),或者只检查每个访问

  • s和n都从cin读取(与n=s.size()相反),如果n>s.size()+2应该触发它
  • n是类型(有符号的)long-long,运算符[]需要一个大小(无符号的long),因此具有i=-1的n-1
  • 将首先尝试访问
    s[-1]
    ,然后将
    i>-1
    作为&&运算符作为从左到右的关联 这是错误的

    while (s[i] == ')' && i > -1)
    
    应该是

    while (i > -1 && s[i] == ')')
    
    如果i等于-1,那么在第一个版本中,
    s[i]
    会给出您可以看到的错误。但是在第二个版本中,首先计算
    i>-1
    ,因为它是false,所以不会计算
    s[i]
    ,因此不会导致错误


    这里还有一个重要的教训。此程序在您的朋友计算机上运行,但并非所有出错的程序都会失败。它甚至可能运行正常。

    您有一个超出范围的索引错误,您是否尝试调试您的程序?但它在我朋友的笔记本电脑上工作相同的代码相同的输入,这是代码中未定义行为的定义。启动你最喜欢的调试器,看看是什么原因造成的。是的,我有我的错误,但我仍然无法找到它是如何在我朋友的计算机上工作的?而且它也被接受了?plzz解释它是如何工作的,是因为不同的编译器版本,比如我的版本是MingW32,他有Mingw64?我不确定什么是不清楚的。行为是未定义的,它可以做任何事情,包括工作。特别是,编译器似乎有额外的检查。其他编译器可能只是接受
    s[-1]
    ,心态是“如果它崩溃,不是我们的错”。是的,我犯了错误,但我仍然无法找到它在我朋友的计算机上是如何工作的?而且它也被接受了?它是如何工作的,Purz解释它是因为不同的编译器版本,像我的是明W 32,他有明W64?@ SuryaAkAt,就像我说的,不是每一个C++程序都有错误失败。C++不这样工作。原因可能是由于编译器的不同,但理论上可能是任何原因。当你编写一个带有这种错误的程序时,C++不能保证它将如何运行。
    while (i > -1 && s[i] == ')')