它没有将字符串作为输入
它将两个整数作为输入,而不是字符串str作为输入 而输入端子仅接受两个输入就停止了
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main(){
int l;
cin >> l;
for(int i=0;i<l;i++){
int w;
cin >> w;
string str;
getline(cin,str);
vector<int> n;
for(int j=0;j<w;j++){
if (str[j] == '1'){
n.push_back(j+1);
}
}
int xw = w/2;
if (n.back() > xw){
cout << n.back()*2 << endl;
}else{
cout << w << endl;
}
}
}
5 2
(程序退出,代码为-1073741819)
答案 0 :(得分:0)
您正在将>>
运算符与getline
混合使用。
您需要清除输入缓冲区,\n
会在其上保留换行符cin >>
。
cin >> w;
cin.ignore(); // this will flush the input buffer
string str;
getline(cin,str);