字梯c程序
c program for word ladder
我写了字梯的C代码。
它正在验证给定的测试用例。但是对于 1 个测试用例,它的输出是错误的,我不知道应该在哪里进行更改。
问题
There is a class of word puzzles where you are given two words, such as BEAK and MAKE, and have to get from one to another by changing one letter at a time. Solving such puzzles requires a good vocabulary and some lateral thinking, but checking the solution once you have one is merely tedious and suitable for a computer to do. Note that even correct solutions are not guaranteed to be minimal.
A solution is correct if, for each pair of adjacent words in the ladder, the following apply:
- they are the same length
- there is exactly one letter changed.
Write a program that will check proposed solutions.
Input Format:
Input will be a solution i.e it consists of a series of words, one per line, terminated
by a line containing a single #. A word is a sequence --- between three and twenty uppercase letters.
Output Format:
Output the word ‘Correct’ or ‘Incorrect’ as appropriate.
Sample Input 1:
BARK
BARE
#
Sample Output 1:
Correct
Sample Input 2:
MAKE
BAKE
BONK
BONE
BANE
#
Sample Output 2:
Incorrect
我的代码
#include<stdio.h>
#include<string.h>
int main()
{
int i,count;
char a[100],b[100],c[100];
int flag=1;
scanf("%s",a);
do
{
scanf("%s",b);
if(b[0]=='#')
break;
if(strlen(a)==strlen(b))
{ i=0,count=0;
while(a[i]!='[=10=]')
{
if(a[i]!=b[i])
count++;
if(count==2)
{
flag=0;
}
i++;
}
}
else
{
flag=0;
}
scanf("%s",a);
if(a[0]=='#')
break;
// scanf("%s",c);
strcpy(c,a);
strcpy(a,b);
strcpy(b,c);
}
while(a[0]!='#');
if(flag==1)
printf("Correct");
else
printf("Incorrect");
return 0;
}
此输入的程序输出不正确
code -> cade -> cate -> date -> data
您读字次数过多。我的意思是,你读的比你比较的多。
看看这个:
- 第一个
scanf()
(进入 a
)不算数。曾经有东西可以比较
- 秒
scanf()
(进入b
)循环
- 循环中
a
和b
的比较
- 循环中的第三个
scanf()
(进入 a
)!!
- 在 2 处重复。
循环内应该只有 1 个 scanf()
和 1 个比较。
你的缩进也很疼
我写了字梯的C代码。 它正在验证给定的测试用例。但是对于 1 个测试用例,它的输出是错误的,我不知道应该在哪里进行更改。
问题
There is a class of word puzzles where you are given two words, such as BEAK and MAKE, and have to get from one to another by changing one letter at a time. Solving such puzzles requires a good vocabulary and some lateral thinking, but checking the solution once you have one is merely tedious and suitable for a computer to do. Note that even correct solutions are not guaranteed to be minimal.
A solution is correct if, for each pair of adjacent words in the ladder, the following apply:
- they are the same length
- there is exactly one letter changed.
Write a program that will check proposed solutions.
Input Format:
Input will be a solution i.e it consists of a series of words, one per line, terminated by a line containing a single #. A word is a sequence --- between three and twenty uppercase letters.
Output Format:
Output the word ‘Correct’ or ‘Incorrect’ as appropriate.
Sample Input 1:
BARK BARE #
Sample Output 1:
Correct
Sample Input 2:
MAKE BAKE BONK BONE BANE #
Sample Output 2:
Incorrect
我的代码
#include<stdio.h>
#include<string.h>
int main()
{
int i,count;
char a[100],b[100],c[100];
int flag=1;
scanf("%s",a);
do
{
scanf("%s",b);
if(b[0]=='#')
break;
if(strlen(a)==strlen(b))
{ i=0,count=0;
while(a[i]!='[=10=]')
{
if(a[i]!=b[i])
count++;
if(count==2)
{
flag=0;
}
i++;
}
}
else
{
flag=0;
}
scanf("%s",a);
if(a[0]=='#')
break;
// scanf("%s",c);
strcpy(c,a);
strcpy(a,b);
strcpy(b,c);
}
while(a[0]!='#');
if(flag==1)
printf("Correct");
else
printf("Incorrect");
return 0;
}
此输入的程序输出不正确
code -> cade -> cate -> date -> data
您读字次数过多。我的意思是,你读的比你比较的多。
看看这个:
- 第一个
scanf()
(进入a
)不算数。曾经有东西可以比较 - 秒
scanf()
(进入b
)循环 - 循环中
a
和b
的比较 - 循环中的第三个
scanf()
(进入a
)!! - 在 2 处重复。
循环内应该只有 1 个 scanf()
和 1 个比较。
你的缩进也很疼