检查数组是否为 'right' (C++)
Checking if an array is 'right' (C++)
A right array
Let's assume we have an array with an N length, made of capital
letters A, B, and C. We call the array 'a right array' if between
every two C letters which come one after another in the array, there
are more A letters than B letters. My job is to discover whether or
not a given array is 'right' and if so, I should print out "RIGHT",
else I should print for how many pieces (places between to Cs) the
given condition is untrue (there are more Bs than As).
Input: In the first line we enter the number of lettes in the array N (1 < N > 200). In the next line we enter the array without empty
spaces in-between.
Output: Print out the answer in a single line.
Examples:
Input: 16
ABBCAABCACBAAACB
Output: RIGHT
Input: 15
ABBCABCACBAAACB
Output: 1
Input: 14
CABCABBCBAABBC
Output: 3
现在,我已经尝试解决这个问题,但第三个示例对我不起作用 - 我得到 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;
}
问题出在案例中
} else if (Ar[j] == 'C') {
i = j;
break;
}
原因是一旦你回到主循环i
就会递增,所以结束C
不会被认为是新组的开始。您的代码基本上是检查每个其他块。
你应该设置
i = j-1;
而不是,因此在递增 i
之后将是 C
的索引。
此外,您应该在评估部分时将 A
和 B
重新初始化为零。
您有几个问题,如下面的代码注释所述:
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;
}
#include <iostream>
using namespace std;
int main() {
int numChars;
cin >> numChars;
char array[numChars];
for (int i = 0; i < numChars; ++i) {
cin >> array[i];
}
int numBrokenPieces = 0;
int numAs = 0;
int numBs = 0;
bool inPiece = false;
for (int i = 0; i < numChars; ++i) {
if (array[i] == 'C') {
if (!inPiece) {
inPiece = true;
continue;
} else {
if (numBs >= numAs) {
++numBrokenPieces;
}
numAs = 0;
numBs = 0;
}
} else {
if (inPiece) {
if (array[i] == 'A') {
++numAs;
} else if (array[i] == 'B') {
++numBs;
}
}
}
}
if (numBrokenPieces == 0) {
cout << "RIGHT";
} else {
cout << numBrokenPieces;
}
return 0;
}
好吧,您也可以采用不同的方法:
string str;
bool counting = false;
int counter = 0, notRightCounter = 0;
cout << "String: ";
cin >> str; // user enters whole string at once
for (char& c : str) { // for each char in string
if (c == 'C') { // start or stop counting when C is found
counting = !counting;
if (!counting && counter <= 0) { // Check if piece between Cs is right
notRightCounter++;
counting = !counting;
}
counter = 0;
continue; // Continue to next char after 'C'
}
if (counting) // Keeping count of A's and B's
switch (c) {
case 'A':
counter++;
break;
case 'B':
counter--;
break;
}
}
// Print results
if (notRightCounter != 0)
cout << "Not right! " << "Not right counter: " << notRightCounter;
else
cout << "Right!";
A right array
Let's assume we have an array with an N length, made of capital letters A, B, and C. We call the array 'a right array' if between every two C letters which come one after another in the array, there are more A letters than B letters. My job is to discover whether or not a given array is 'right' and if so, I should print out "RIGHT", else I should print for how many pieces (places between to Cs) the given condition is untrue (there are more Bs than As).
Input: In the first line we enter the number of lettes in the array N (1 < N > 200). In the next line we enter the array without empty spaces in-between.
Output: Print out the answer in a single line.
Examples:
Input: 16 ABBCAABCACBAAACB Output: RIGHT
Input: 15 ABBCABCACBAAACB Output: 1
Input: 14 CABCABBCBAABBC Output: 3
现在,我已经尝试解决这个问题,但第三个示例对我不起作用 - 我得到 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;
}
问题出在案例中
} else if (Ar[j] == 'C') {
i = j;
break;
}
原因是一旦你回到主循环i
就会递增,所以结束C
不会被认为是新组的开始。您的代码基本上是检查每个其他块。
你应该设置
i = j-1;
而不是,因此在递增 i
之后将是 C
的索引。
此外,您应该在评估部分时将 A
和 B
重新初始化为零。
您有几个问题,如下面的代码注释所述:
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;
}
#include <iostream>
using namespace std;
int main() {
int numChars;
cin >> numChars;
char array[numChars];
for (int i = 0; i < numChars; ++i) {
cin >> array[i];
}
int numBrokenPieces = 0;
int numAs = 0;
int numBs = 0;
bool inPiece = false;
for (int i = 0; i < numChars; ++i) {
if (array[i] == 'C') {
if (!inPiece) {
inPiece = true;
continue;
} else {
if (numBs >= numAs) {
++numBrokenPieces;
}
numAs = 0;
numBs = 0;
}
} else {
if (inPiece) {
if (array[i] == 'A') {
++numAs;
} else if (array[i] == 'B') {
++numBs;
}
}
}
}
if (numBrokenPieces == 0) {
cout << "RIGHT";
} else {
cout << numBrokenPieces;
}
return 0;
}
好吧,您也可以采用不同的方法:
string str;
bool counting = false;
int counter = 0, notRightCounter = 0;
cout << "String: ";
cin >> str; // user enters whole string at once
for (char& c : str) { // for each char in string
if (c == 'C') { // start or stop counting when C is found
counting = !counting;
if (!counting && counter <= 0) { // Check if piece between Cs is right
notRightCounter++;
counting = !counting;
}
counter = 0;
continue; // Continue to next char after 'C'
}
if (counting) // Keeping count of A's and B's
switch (c) {
case 'A':
counter++;
break;
case 'B':
counter--;
break;
}
}
// Print results
if (notRightCounter != 0)
cout << "Not right! " << "Not right counter: " << notRightCounter;
else
cout << "Right!";