2

考虑一个将字母转换为整数的编码系统,其中“a”表示为 1,“b”表示为 2,..“z”表示为 26。给定一个数字数组(1 到 9)作为输入,编写一个打印所有输入数组的有效解释。

/*Examples

Input: {1, 1}
Output: ("aa", 'k") 
[2 interpretations: aa(1, 1), k(11)]

Input: {1, 2, 1}
Output: ("aba", "au", "la") 
[3 interpretations: aba(1,2,1), au(1,21), la(12,1)]

Input: {9, 1, 8}
Output: {"iah", "ir"} 
[2 interpretations: iah(9,1,8), ir(9,18)]*/

我的c代码

#include<iostream>
using namespace std;
#include<string.h>
int a[10]={2,3,4,4,2,4,2,8,9};
char c[]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};

void func(int i,char result[10])
{
    if(i==10)
    {
       int l=strlen(result);
       for(int j=0;j<l;j++)
           cout<<result[j];
    }

    else
    {
        if(10*a[i]+a[i+1]<26)
        {
            strcat(result,"c[10*a[i]+a[i+1]]");
            func(i+2,result);
        }

        strcat(result,"c[a[i]]");
        func(i+1,result);
    }
}

int main()
{

    func(0,"");
}

我无法找出错误。你能帮我吗??

4

4 回答 4

2
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int a[9]={2,3,4,4,2,4,2,8,9};
char c[]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};

void func(int i,char result[11])
{
    if(i==10)
    {
      printf ("%s\n", result);
    }
    else
    {
        char * temp = (char *)malloc(11);
        sprintf(temp, "%s%c", result, c[a[i] - 1]);
        func(i+1,temp);
        free(temp);

        if(i < 9 && 10*a[i]+a[i+1] < 26)
        {
            char * temp = (char *)malloc(11);
            sprintf(temp, "%s%c", result, c[10*a[i]+a[i+1] - 1]);
            func(i+2, temp);
            free (temp);
        }
    }
}
int main()
{
    func(0,"");
}

输出

bcddbdbhi
bcddxbhi
wddbdbhi
wddxbhi

您的代码潜在的致命问题

  1. strcat(result,"c[10*a[i]+a[i+1]]");并且strcat(result,"c[a[i]]");您将连接那些result大小仅为 10 的字符串,而不是连接与该数字对应的字符。

其他问题

  1. 在里面if(10*a[i]+a[i+1]<26)你正在更改 的内容result,当递归结束时,结果的值与它进入函数时的值不同。因此最好在内部创建新字符串并在任务结束时释放它们。

建议

  1. 尽量不要混合使用 C 和 C++ 函数。
于 2013-09-10T14:11:36.580 回答
1

我看到的一个错误是您的结果数组可能没有 10 个字符。

for(int j=0;j<10;j++)
cout<<result[j];

这将导致分段错误。

编辑:如果您愿意使用 C++ 元素,那么我更喜欢使用 std::string 而不是 char 数组来获取结果。

于 2013-09-10T14:06:22.740 回答
1

您询问了错误。开始:

  1. 不要“使用命名空间”放在其他包含之前,这可能会导致其他项目中的编译器出现非常奇怪的错误。
  2. a[10] 未定义,但在调用 func(9, result) 时进行评估;
  3. 此外,调用 func(9, result) 可能会导致调用 func(9+2, result)。
  4. strcat(结果,"c[10*a[i]+a[i+1]]"); 将字符串 "c[10*a[i]+a[i+1]]" 添加到结果中。我怀疑这是你想要的结果。此外,编译器应该建议您使用 strncat() 代替。
  5. 最好使用 std::string 和 std::stringstream,除非你有充分的理由避免使用它们。
  6. 变量a在 func() 中使用,但不作为参数传递。一般来说,这不是一个好主意。

示例代码(这使用了一些其他技巧):

    void PrintDecodings(const int* a, size_t length, std::string acc){
        static const char* alphabet = "0abcdefghijklmnopqrstuvwxyz";
        if(length == 0){
            std::cout << acc << std::endl;
            return;
        }
        if(length == 1){
            std::cout << acc << alphabet[*a] << std::endl;
            return;
        }
        if(10*a[0]+a[1] <= 26){ //At this point, length>=2, so a[1] is OK
            PrintDecodings(a+2, length-2, acc+alphabet[10*a[0]+a[1]]);
        }
        return PrintDecodings(a+1, length-1, acc+alphabet[*a]);
    }

    //...
        PrintDecodings(a, 9, "");
于 2013-09-11T06:21:05.537 回答
0
#include<iostream>
using namespace std;
#include<string.h>
int a[3]={1,2,1};
char c[]={'NULL','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};

void func(int i,char result[3],int r)
{
    if(i==3)
    {
      for(int j=0;j<r;j++)
           cout<<result[j];
       cout<<"\n";
       return;
    }
    if(10*a[i]+a[i+1]<26)
    {
      result[r]=c[10*a[i]+a[i+1]];
      func(i+2,result,r+1);
    }
    result[r]=c[a[i]];
    func(i+1,result,r+1);

}


int main()
{
    char result[10];
    func(0,result,0);
}
于 2013-09-14T14:02:37.460 回答