现在,我尝试解决此问题,但是第三个示例对我不起作用-我得到2的输出,如上所述,我应该得到3,除此之外-编译起来很好。
#include <iostream>
using namespace std;
int main()
{
int N;
cin >> N;
char Ar[N];
int A = 0;
int B = 0;
int piece = 0;
int attempt = 0;
for (int i = 0; i < N; i++) {
cin >> Ar[i];
}
for (int i = 0; i < N; i++) {
if (Ar[i] == 'C') {
for (int j = i + 1; i < N; j++) {
if (Ar[j] == 'A') {
A++;
} else if (Ar[j] == 'B') {
B++;
} else if (Ar[j] == 'C') {
i = j;
break;
}
}
if (A > B) {
piece++;
attempt++;
} else if (A <= B) {
attempt++;
}
A = 0;
B = 0;
}
}
if (piece == attempt) {
cout << "RIGHT";
} else {
cout << attempt - piece;
}
return 0;
}
最佳答案
如下面的代码注释中所述,您遇到了一些问题:
int N;
cin >> N;
std::vector<char> Ar(N);
for (int i = 0; i < N; i++) {
cin >> Ar[i];
}
int piece = 0;
int attempt = 0;
for (int i = 0; i < N - 1; i++) {
if (Ar[i] != 'C') {
// Skip letters until the first C
continue;
}
int A = 0;
int B = 0;
int j = i + 1;
for (; j < N; j++) {
if (Ar[j] == 'A') {
A++;
} else if (Ar[j] == 'B') {
B++;
} else if (Ar[j] == 'C') {
// We only account for blocks between Cs
attempt++;
if (A > B) {
piece++;
}
break;
}
}
// Next piece starts at j, i will be incremented by outer loop
i = j - 1;
}
关于c++ - 检查数组是否为 'right'(C++),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54961811/