gpt4 book ai didi

c++ - USACO-beads,无法调试 while 循环

转载 作者:行者123 更新时间:2023-11-28 04:32:36 26 4
gpt4 key购买 nike

This是我正在研究的问题

我没有收到任何编译时错误、运行时错误,而且我的大部分代码似乎都可以正常工作。调试后,我发现 left()right() 中的 while 循环导致了问题。我很难弄清楚为什么,所以任何帮助都会很棒。谢谢。

以防万一--

Sample input:

29

wwwbbrwrbrbrrbrbrwrwwrbwrwrrb

Sample output:

11

代码:

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000000 + 10;
int n;
char cur_beads[MAXN];

//WORKS LIKE A BOSS:
void final_array(int int_in, char * array_in){

for (int i = 1; i <= int_in; i++){
cur_beads[n-i] = array_in[int_in - i];
}

for (int i = 0; i < n - int_in; i++){
cur_beads[i] = array_in[int_in+i];
}
// cout << "new array, for "<< int_in << " = " <<cur_beads << endl << endl;;
}
//ERROR HERE:
int left(){ //function
int left_a = 0, left_b = 0, c = 0;

cout << cur_beads << endl;

while((cur_beads[c] != 'b' || cur_beads[c] != 'w') && c <= n){
cout << cur_beads[c] << endl;
c++;
}
cout << "c = " << c << endl;
while((cur_beads[c] == 'b' || cur_beads[c] == 'w')){
left_b++;
c++;
}
cout << "left_b = " << left_b << endl;

c = 0;
while((cur_beads[c] != 'r' || cur_beads[c] != 'w') && c <= n){
c++;
}
cout << "c = " << c << endl;

while(cur_beads[c] == 'r' || cur_beads[c] == 'w'){
left_a++;
c++;
}
cout << "left_a = " << left_a << endl;

if (left_a > left_b){
return left_a;
cout << "left = " << left_a << endl;
}
else{
return left_b;
cout << "left = " << left_b << endl;
}
}
//ERROR HERE:
int right(){ //function
int right_a = 0, right_b = 0, c = 0;

while((cur_beads[n-c] != 'b' || cur_beads[n-c] != 'w') && c <= n){
c++;
}
while((cur_beads[n-c] == 'b' || cur_beads[n-c] == 'w')){
right_b++;
c++;
}

c = 0;
while((cur_beads[n-c] != 'a' || cur_beads[n-c] != 'w') && c <= n){
c++;
}
while((cur_beads[n-c] == '' || cur_beads[n-c] == 'w')){
right_a++;
c++;
}

if (right_a > right_b){
return right_a;
cout << "right = " << right_a << endl;
}
else{
return right_b;
cout << "right = " << right_b << endl;
}
}

int main(){
int ans = 0, current_ans = 0; //stored as 0 by default

ifstream fin("beads.in");
ofstream fout("beads.out");

fin >> n;

char beads[MAXN];


for(int i = 0; i < n; i++){
fin >> beads[i];
}

// final_array(11, beads);
// current_ans = left();
// cout << "current_ans = " << current_ans << endl;


for(int i = 1; i < n; i++){ //main loop
final_array(i, beads);
current_ans = left() + right();
cout << "current_ans = " << current_ans << endl;

if (current_ans > ans){
ans = current_ans;
}
}

fout << ans << endl;

}

left() 中的所有 cout 都用于调试代码。

最佳答案

线

while((cur_beads[c] != 'b' || cur_beads[c] != 'w') &&  c <= n)

没有意义,因为任何字符都不会既是“b”又是“w”。更有意义的是将此行(以及其他有此问题的行)修改为

while((cur_beads[c] != 'b' && cur_beads[c] != 'w') &&  c <= n)

关于c++ - USACO-beads,无法调试 while 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52454232/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com