【发布时间】:2021-08-21 14:04:15
【问题描述】:
我有一些规则可以在 char 数组中创建字符串
a. The string begins with an 'a'
b. Each 'a' is followed by nothing or an 'a' or "bb"
c. Each "bb" is followed by nothing or an 'a'
我的代码是:-
bool checkAB(char input[]) {
if(input[0]=='\0'){
return true;
}
if(input[0]!='a'){
return false;
}
bool ans=checkAB(input+1);
if(input[0]=='a'){
if(input[1]=='a' || (input[1]=='b' && input[2]=='b') || input[1]==' ')
return ans && true;
if(input[1]=='b' && input[2]=='b'){
if(input[3]==' ' || input[3]=='a'){
return ans && true;
}
}
}
return false;
}
这段代码没有通过所有的测试用例。谁能帮忙。 请使用递归方法
【问题讨论】:
-
看起来像家庭作业。是吗?阅读此open letter to students with homework problems。
-
input[1]==' '测试空间。空间不是什么。显示原始问题陈述。 -
bool ans=checkAB(input+1);尝试以递归方式检查输入,但后面的代码也检查输入。这段代码的结构不正确,纠正它并不是修复小错误的简单问题。很可能,代码应该从头开始重写。 -
if(input[0]=='\0'){ return true; }使函数接受一个有效的空字符串。但是,规则 a。表示字符串必须以“a”开头,而空字符串不以“a”开头。显示原始问题陈述。
标签: c recursion c-strings function-definition