我在我的大学上 C 课程,我认为我仍然停留在 JAVA 上,因为我无法获得下一个代码(递归):
#include <stdio.h>
#include <conio.h>
#define N 11
int Combination(char *, char *, char *);
void main(){
int i;
char *S1[]={"","abc","abc","abc","abc","ab c","morning Venm","ABZ","12189","12189",
"TTTT"},
*S2[]={"", "", "def", "def", "def", "def", "Good ita!", "ABAXZ", "129", "129",
"X"},
*S3[]={"", "abc", "abcdef", "daebcf", "adfbce", "deab cf","Good morning Vietnam!",
"ABAXABZZ", "12181299", "12112998", "XXXXX"};
for(i=0;i<N;i++){
if(Combination(S1[i],S2[i],S3[i]))
printf("S1: \"%s\", S2: \"%s\", S3: \"%s\",
Combination!\n",S1[i],S2[i],S3[i]);
else printf("S1: \"%s\", S2: \"%s\", S3: \"%s\", Not a
Combination!\n",S1[i],S2[i],S3[i]);
}
_getch();
}
/*Function name : Combination
Input : address of three strings
Output : true(1) if the third string is a combination of the two firsts strings,
false (0) if not
Algorithm : check if the third string is made from the letters of the first and
second strings*/
int Combination(char *S1, char *S2, char *S3)
{
if(!*S1 && !*S2 && !*S3) return 1;
if(*S3==*S1 && *S3==*S2)
return (Combination(S1+1,S2,S3+1)||Combination(S1,S2+1,S3+1));
if(*S3==*S1) return Combination(S1+1,S2,S3+1);
if(*S3==*S2) return Combination(S1,S2+1,S3+1);
return 0;
}
我想了解组合方法的任何一行。
1) if(!*S1 && !*S2 && !*S3) = 检查 3 个字符串是否为空?
2)什么部分:(S1 + 1,S2,S3 + 1) - 那在做什么?S1+1 会给我们数组上的下一个单词还是给出下一个字母?如果它会给我们下一封信 - 为什么?如果字符串相等,它已经检查过了?
我很困惑...
- 我得到了递归,但不是 S1+1\S2+1\S3+1 的一部分...