学习到的点 1 如何获取字符串中指定字符或者指定字符的的索引?
答:引入vector 创建一个vector容器 遍历字符串时候 将符合条件的元素(或者元素序号)通过.push_back()方法 添加到 容器当中 这样就提炼出来一个需要的容器 再通过容器的索引 获取相关元素的序号
2需要注意的点 if (b[j] == '1') 而不是 if (b[j] == 1) 不然会出现段错误
2
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main(){
int T = 0 ;
cin >> T ;
for (int i=0;i< T ; i++){
int n = 0;string b;
vector<int> X;
cin >> n ;
cin >> b;
for(int j = 0; j < n; j++){
if ( b[j] == '1' ){
X.push_back(j);
}
}
int distance1 = X[1] - X[0];
int distance2 = X[2] - X[1];
if (distance1 == distance2){
cout << "Yes" <<endl;
}
else cout << "No" <<endl;
}
return 0;
}