【问题标题】:basic resursion que of string [closed]字符串的基本递归队列[关闭]
【发布时间】: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


【解决方案1】:

考虑用“abb”调用时会发生什么

第一次调用会执行这一行:

bool ans=checkAB(input+1);

这意味着我们用输入“bb”调用函数

这将返回 FALSE(由于 if(input[0]!='a'))。

因此,在第一次调用中,我们将 ans 设置为 FALSE,因此我们最终返回 FALSE。

我认为递归调用之前,您应该删除“a”之后的所有“bb”。

类似:

bool checkAB(const char input[]) 
{
    if(input[0] != 'a') return false;   // input must start with an 'a'

    if (input[1] == '\0') return true;  // input was "a"

    if (input[1] == 'b' && input[2] == 'b')
    {
        if (input[3] == '\0') return true;   // input was "abb"
        
        return checkAB(input+3);  // Call without leading "abb"
    }

    return checkAB(input+1);  // Call without leading "a"
}

或者你可以使用strcmp 喜欢:

bool checkAB(const char input[]) 
{
    if (input[0] != 'a') return false;
    
    if (strcmp(input, "a") == 0) return true;
    if (strcmp(input, "abb") == 0) return true;

    if (input[1] == 'b' && input[2] == 'b') return checkAB(input+3);  // Call without leading "abb"

    return checkAB(input+1);  // Call without leading "a"
}

【讨论】:

  • 你能解释一下吗 if (input[1] != 'b' || input[2] != 'b'){ return false; }
  • @KishanRaj 它检查两个连续的b
  • @KishanRaj 我将答案更新为更易读的形式。希望对您有所帮助。
【解决方案2】:

对于初学者来说,函数参数应该有限定符const,因为传递的字符串不会在函数内改变

bool checkAB( const char input[]) {

空字符串不满足要求。所以这个 if 语句

    if(input[0]=='\0'){
        return true;
    }

不正确。

因此调用

bool ans=checkAB(input+1);

可以将变量ans 设置为false,例如当源字符串为"a" 时。在这种情况下,函数返回 false。

还不清楚为什么要将字符串的元素与空格字符进行比较

 if(input[1]=='a' || (input[1]=='b' && input[2]=='b') || input[1]==' ')
                                                         ^^^^^^^^^^^^^

该函数可以如下面的演示程序所示。

#include <stdio.h>
#include <stdbool.h>

bool checkAB( const char *s ) 
{
    return  *s == 'a' && 
            ( *++s == '\0' || 
              ( *s == 'a' && checkAB( s ) ) ||
              ( *s == 'b' && *++s == 'b' && ( ( *++s == '\0' ) || ( *s == 'a' && checkAB( s ) ) ) ) );
}

int main(void) 
{
    const char *s = "a";
    
    printf( "\"%s\" -> %s\n", s, checkAB( s ) ? "true" : "false" );
    
    s = "aa";
    
    printf( "\"%s\" -> %s\n", s, checkAB( s ) ? "true" : "false" );

    s = "abb";
    
    printf( "\"%s\" -> %s\n", s, checkAB( s ) ? "true" : "false" );
    
    s = "aaa";
    
    printf( "\"%s\" -> %s\n", s, checkAB( s ) ? "true" : "false" );

    s = "abba";
    
    printf( "\"%s\" -> %s\n", s, checkAB( s ) ? "true" : "false" );
    
    return 0;
}

程序输出是

"a" -> true
"aa" -> true
"abb" -> true
"aaa" -> true
"abba" -> true

【讨论】:

  • *s == 'a' &amp;&amp; checkAB( s ) 是多余的; checkAB(s) 就够了。测试用例不包括任何字符串不满足规则的用例。
  • @EricPostpischil 不,这不是多余的。指针 s 在此条件之前递增。
  • 这是多余的,因为*s == 'a' &amp;&amp; checkAB(s) 为真当且仅当checkAB(s) 为真。 s 之前是否增加无关紧要;所涉及的问题不在于*s == 'a'checkAB(s) 是否受s 指向的任何先前字符的影响,而是它们的值与当前s 的关系。 checkAB(s) 只有在 *s == 'a' 为真时才计算为真,因为这是其中的第一个代码,所以checkAB(s) 永远不会与*s == 'a' &amp;&amp; checkAB(s) 不同。
  • @EricPostpischil 如果只是为了编写 checkAB(s),那么可以为以“bb”开头的字符串调用函数。在这种情况下,此调用返回 false。
  • @VladfromMoscow: 是的,当checkAB(s)s 指向“bb”时,你想要的结果是false。如果在*++s == '\0' 之后,我们调用checkAB(s)s 指向“bb”,那么checkAB 将返回false,代码将继续|| 并评估*s == 'b' &amp;&amp;… 并得到所需的结果.
猜你喜欢
  • 2016-06-25
  • 2016-04-30
  • 1970-01-01
  • 2014-02-22
  • 2014-08-11
  • 1970-01-01
  • 1970-01-01
  • 2018-02-25
  • 1970-01-01
相关资源
最近更新 更多