这是我要解决的问题:
输入:
第一行包含 N,字符串的大小。
第二行包含字母(仅小写)。
输出:
如果在字符串中找到所有元音字母,则打印 YES
,否则打印 NO
。
约束:
字符串的大小不会大于 10,000。 1 ≤ N ≤ 10000
我写的下面的代码总是显示NO
。
#include <stdio.h>
#include<conio.h>
int main()
{
int a,b,c=0,d=0,e=0,f=0,g=0,i;
char string[10000];
scanf("%d",&a);
scanf("%s",string);
for(i=0;i<a;a++)
{
if(string[i]==('a'))
c=1;
if(string[i]==('e'))
d=1;
if(string[i]==('i'))
e=1;
if(string[i]==('o'))
f=1;
if(string[i]==('u'))
g=1;
}
if((c==1)&&(d==1)&&(e==1)&&(f==1)&&(g==1))
printf("YES");
else
printf("NO");
return 0;
getch ();
}
最佳答案
这是一个导致问题的无限循环:
for(i=0;i<a;a++)
您应该增加 i,而不是 a(字符串的长度)。如果你在循环语句中修复这个字符,程序将运行良好。无论如何,我稍微更改了您的代码以提高可读性。如果你想看一看,仅供引用,先生:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int len, a=0, e=0, i=0, o=0, u=0, it;
char string[10000];
scanf("%d", &len);
scanf("%s", string);
for(it=0;it<len;it++)
{
if(string[it]=='a') a = 1;
else if(string[it]=='e') e = 1;
else if(string[it]=='i') i = 1;
else if(string[it]=='o') o = 1;
else if(string[it]=='u') u = 1;
}
if(a && e && i && o && u) printf("YES\n");
else printf("NO\n");
system("PAUSE");
return 0;
}
我假设你在 Windows 下运行你的程序,所以而不是 conio 的 getch()
尝试使用 system("PAUSE")
或者更好的方法来做这(对于 UNIX 和 Windows):getchar()
关于检查字符串中元音的出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32273655/