标签:div turn 数据 方式 规划 http png com oid
1 #include<stdio.h> 2 #include<string.h> 3 void convert(char str[]); 4 void add(char str[]); 5 int main(void) 6 { 7 char ch1[15]; 8 char ch2[1000020]; 9 int i=0,j=0; 10 int word=1,flag=1; 11 int num=0; 12 int temp=-1; 13 14 gets(ch1); 15 gets(ch2); 16 convert(ch1); 17 convert(ch2); 18 add(ch2); 19 while(ch2[i]!=‘\0‘) 20 { 21 if(ch2[i]==‘ ‘) 22 { 23 word=0;flag=1;j=0; 24 } 25 else word=1; 26 if(word==1&&flag==1) 27 { 28 if(ch2[i]==ch1[j]) 29 { 30 flag=1; j++; 31 if(ch1[j]==‘\0‘&&ch2[i+1]==‘ ‘) 32 { 33 num++; 34 if(temp==-1) temp=i-strlen(ch1)+1; 35 } 36 } 37 else 38 { 39 flag=0;j=0; 40 } 41 } 42 i++; 43 } 44 printf("%d ",num==0?-1:num); 45 if(num!=0) printf("%d",temp-1); 46 47 return 0; 48 } 49 void convert(char str[]) 50 { 51 int i=0; 52 while(str[i]!=‘\0‘) 53 { 54 if(str[i]>=‘A‘&&str[i]<=‘Z‘) str[i]+=32; 55 i++; 56 } 57 } 58 void add(char str[]) 59 { 60 int i; 61 int temp=strlen(str); 62 for(i=temp-1;i>=0;i--) 63 { 64 str[i+1]=str[i]; 65 } 66 str[0]=‘ ‘; 67 str[temp+1]=‘ ‘; 68 str[temp+2]=‘\0‘; 69 }
这题困了我一上午!!一上午!!
此题也规范了我解决问题的方式。一开始写的时候想到啥就写啥,想着留着最后debug的时候完善。最后发现debug太难了!!
第二次我就先把算法大致写了一下,很快就写出来了比较完整的代码。但是最后还是WA了几组数据。之后又随意试了几组数据,没想到竟然试出来啦~我用temp记录找到的第一个单词的首字母的下标时,一开始直接把相同的首字母的下标赋值给了temp,忽视了此时的单词可能并不符合题意。
比如 我想找 在 a t i to里面找to,显然刚开始的temp就被我给赋值为3了 但是t不是to。
刷题时,还是要保持一个清晰的头脑,提前做好规划,每一步都要干什么,这样写代码就会比较顺利。
加油,Ziyang!!
标签:div turn 数据 方式 规划 http png com oid
原文地址:https://www.cnblogs.com/ziyang1060/p/11929316.html