首页 > 其他 > 详细

NOIP2011统计单词数

时间:2015-07-24 23:50:42      阅读:314      评论:0      收藏:0      [点我收藏+]
技术分享
 
 
技术分享
 
 
分析:常规题,一次扫描即可,但要注意大小写和没出先要输出-1.判断是否是某个单词的一部分可以通过判断后一个词是不是空格或\0来解决。上题。
 1 #include <stdio.h>
 2 #include<string.h>
 3 #define MAX 1000
 4 int main()
 5 {
 6     char str[MAX];
 7     char word[MAX];
 8     int i,lenth,j=0;    
 9     int t,L;
10     int num=0,pos;
11      gets(word);
12      gets(str);
13     lenth=strlen(str);        
14     L=strlen(word);
15      for(i=0;i<lenth;i++)
16      {
17          if(str[i]==word[j]||(str[i]+32)==word[j]||(str[i]-32)==word[j])        //大小写一样也可以 
18          {
19              j++;
20              if(j==1) t=i;    //标记第一次的位置 
21          }
22          else
23          {
24              j=0;        //不是该单词清零 
25          }
26          if(j==L&&(str[i+1]== ||str[i+1]==\0))    //判断是不是某词的一部分 
27          {
28              if(num==0) pos=t;
29              num++;
30          }
31      }
32      if(num==0) printf("-1");
33      else printf("%d\n%d",num,pos);
34     return 0;
35 }

 

NOIP2011统计单词数

原文:http://www.cnblogs.com/charlotteandxing/p/4674962.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!