求助,恳请各位帮忙.....
各位好。我在用语言C编写最长公共子序列问题的时候...遇到了的问题。在赋值计算的时候,总是出现很奇怪的数字。
请各位帮个忙。
程序如下:
nclude<stdio.h>
main()
{ int i,j,m,n,k,p;
char x[100],y[100];
int b[50][50],c[50][50];
printf("Please input how many strings1 you want to compare:");
scanf("%d",&m);
printf("Please input how many strings2 you want to compare:");
scanf("%d",&n);
printf("\n");
printf("Please input the string1:");
printf("\n");
for(i=0;i<2*m;i++)
scanf("%c",&x[i]);
printf("\n");
printf("Please input the string2:");
printf("\n");
for(i=0;i<2*n;i++)
scanf("%c",&y[i]);
for(i=1;i<=m;i++) c[i][0]=0;
for(i=1;i<=n;i++) c[0][i]=0;
k=-1;
for(i=1;i<=m;i++)
{ k=k+2;
p=-1;
for(j=1;j<=n;j++)
{
p=p+2;
if(x[k]==y[p])
{
c[i][j]=c[i-1][j-i]+1;
b[i][j]=1;
}
else if(c[i-1][j]>=c[i][j-1])
{
c[i][j]=c[i-1][j];
b[i][j]=2;
}
else
{
c[i][j]=c[i][j-1];
b[i][j]=3;
}
}
}
for(i=0;i<=n;i++)
{
printf("%d ",c[2][i]);
}
printf("\n");
for(i=1;i<=n;i++)
{
printf("%d ",b[2][i]);
}
return 0;
}
运行程序的时候,当我一次输入
7
6
A B C B D A B
B D C A B A
的时候 正确的答案应该是0 1 1 1 1 2 2
1 3 3 2 1 3
但是我这个程序却出现一个很大的数字
我想不明白是什么原因
请各位帮个忙~谢谢大人们.. 同时~我在编程的时候
是获取键盘输入的字符的 然后放入字符数组中
但是,发现个很奇怪的地方
假如我输入的字符是ABC 照道理说,把ABC放进数组里面,数组长度应该是3,但是在程序运行的时候,数组的长度却是6。
所以,以上的程序我用了k和p来标记数组下标
每两个长度代表一个字母
请殿下们指教一下出现这种问题的原因是什么呢? 很久不用C啦[tk01]
页:
[1]
