一开始是这样的代码,出现了TLE
#include<stdio.h>
int main(){
char per;
int sum=0;
scanf("%c",&per);
while(per!='\n'){
if(per=='c'){
scanf("%c",&per);
if(per=='h'){
scanf("%c",&per);
if(per=='u'){
scanf("%c",&per);
if(per=='a'){
scanf("%c",&per);
if(per=='n'){
scanf("%c",&per);
if(per=='z'){
scanf("%c",&per);
if(per=='h'){
scanf("%c",&per);
if(per=='i'){
sum++;
}
}
}
}
}
}
}
}
scanf("%c",&per);
}
printf("%d",sum);
return 0;
}
在我电脑上跑,总是要过好久才能显示出来,想不明白为什么速度会这么慢。 之后我把代码改为了
#include<stdio.h>
#include<string.h>
int main(){
int main(){
char a[400001],per=0;
int sum=0;
memset(str,0,sizeof(str));
//gets(str);
scanf("%s",a);
while(str[per]!='\0'&&per<=sizeof(str)-6){
{
/*if(str[per]=='c'){
if(str[per+1]=='h'){
if(str[per+2]=='u')
if(str[per+3]=='a'){
if(str[per+4]=='n')
if(str[per+5]=='z'){
if(str[per+6]=='h')
if(str[per+7]=='i'){
sum++;
}
}
}
}
}*/
}
跑出来有大问题,并且gets好像不能用了 之后,我想是不是if嵌套的太多了,就用(这里换了一下数组名)
for(int i=0;i<(int)(strlen(a)-8);i++)if(a[i]=='c'&&a[i+1]=='h'&&a[i+2]=='u'&&a[i+3]=='a'&&a[i+4]=='n'&&a[i+5]=='z'&&a[i+6]=='h'&&a[i+7]=='i')sum++;
还是过不了。 求各路大神救救这个孩子。