检查数组是否“正确”(C ++)

问题描述 投票:5回答:4

一个正确的数组

假设我们有一个N长度的数组,由大写字母A,B和C组成。如果在数组中一个接一个地出现的每两个C字母之间,我们将数组称为“右数组”,还有更多A字母比B字母。我的工作是发现一个给定的数组是否“正确”,如果是这样,我应该打印出“正确”,否则我应该打印多少件(Cs之间的位置)给定的条件是不真实的(还有更多) Bs比As)。

输入:在第一行中,我们输入数组N中的字母数(1 <N> 200)。在下一行中,我们输入数组,中间没有空格。

输出:在一行中打印答案。

例子:

  • 输入:16 ABBCAABCACBAAACB输出:右
  • 输入:15 ​​ABBCABCACBAAACB输出:1
  • 输入:14 CABCABBCBAABBC输出: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;
}
c++ arrays char element error-checking
4个回答
2
投票

您有几个问题,如下面的代码注释中所述:

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;
}

3
投票

问题出在这种情况

        } else if (Ar[j] == 'C') {
            i = j;
            break;
        }

原因是一旦你回到主循环,i将增加,所以结束C将不被视为新组的开始。您的代码基本上是检查每个其他块。

你应该设置

i = j-1;

相反,所以增加i后将成为C的指数。

此外,在评估某个部分时,您应该将AB重新初始化为零。


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;
}

1
投票

好吧,你也可以稍微改变一下:

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!";
© www.soinside.com 2019 - 2024. All rights reserved.