【发布时间】:2013-09-10 13:46:42
【问题描述】:
考虑一个将字母转换为整数的编码系统,其中“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,"");
}
我无法找出错误。你能帮帮我吗?
【问题讨论】:
-
你看到了什么错误?
-
"using namespace std" 几乎不是“你的 C 代码”。我假设你的意思是 C++?
-
另外,您正在尝试将 strcat 转换为字符串文字,该文字不够短且只读。
-
Vaughn Cato-- 这是一个运行时错误(分段错误)
-
H2CO3--是的,我的意思是 c++ 而已
标签: c++ algorithm recursion dynamic-programming