zcmu 1091

1091: 统计单词

Description

输入一个正整数 repeat (0<repeat<10),做repeat 次下列运算:
输入一行字符,统计其中单词的个数。各单词之间用空格分隔,空格数可以是多个。

Input

见sample

Output

见sample

Sample Input

3 Every night in my dreams I see you I feel you That is how I know you go on

Sample Output

5 6 8
思路:用一个bool数组,如果遇到字母就赋值true,别的为false。遍历bool数组,如果遇到一串连续的true就把ans++,最后输出ans。注意输入字符串的格式,gets遇到空行便会结束,因此需要在scanf后面用一个getchaar()平衡空行。
代码如下:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<time.h>
using namespace std;
#define FORA(i,x,y) for(int i = x; i < y; i++)
#define FORB(i,x,y) for(int i = x; i <= y; i++)
#define FORC(i,y,x) for(int i = y; i > x; i--)
#define maxn 100000
#define INF 1000000000
#define LL long long
const int mod = 1000000;
char a[maxn];

int main(){
    int t;
    scanf("%d",&t);
    getchar();
    while(t--){
         gets(a);
         bool f[maxn];
         memset(f,false,sizeof(f));
         int t = strlen(a);
         FORA(i,0,t){
            if(a[i] != ' '){
                f[i+1] = true;
            }
         }
         int ans = 0;
         FORB(i,1,t){
            if(f[i] == true && f[i-1] == false){
                ans++;
            }
         }
         printf("%d\n",ans);
    }
    return 0;
}


 
posted @ 2018-08-08 14:35  半忧夏  阅读(222)  评论(0)    收藏  举报