zoukankan      html  css  js  c++  java
  • 寒假刷题之3——小朋友来数数

    Kindergarten Counting Game

    Everybody sit down in a circle. Ok. Listen to me carefully.

    ``Woooooo, you scwewy wabbit!''

    Now, could someone tell me how many words I just said?

    Input and output

    Input to your program will consist of a series of lines, each line containing multiple words (at least one). A ``word'' is defined as a consecutive sequence of letters (upper and/or lower case).

    Your program should output a word count for each line of input. Each word count should be printed on a separate line.


    Input


    Meep Meep!

    I tot I taw a putty tat.
    I did! I did! I did taw a putty tat.
    Shsssssssssh ... I am hunting wabbits. Heh Heh Heh Heh ...


    Output


    2

    7
    10
    9



       复制这些内容就够蛋疼啊。。。

       就是普通的计算单词数目,以前好像写过,很快就写了一段:

    #include<stdio.h>
    
    int main()
    {
    	char letter;
    	int i = 0;
    	
    	for (; (scanf("%c", &letter)) != EOF; letter == ' ' ? i ++ : NULL)
    		letter == '\n'? (printf("%d\n", i + 1), i = 0) : NULL;
    	
    	printf("%d\n", i + 1);
    	
    	return 0;
    }

      传上去竟然Wrong answer了。。。

      好吧我认真地看了原题目,连续的字母算一个单词,我是按空格数+1来算的。

      改代码吧。。。

      完全改掉代码,换思路来写,用isalpha判断第一个字符是不是字母,然后读取后面的字母直到其他字符,继续累积。

      没想到还给wa了两次,都是我不小心,忘记把i初始化。

      现在终于ac了,虽然不是很好的代码。。。贴出代码:

    #include<stdio.h>
    #include<ctype.h>
    
    int main()
    {
    	char letter;
    	int i = 0;
    	
    	for (; (scanf("%c", &letter)) != EOF;){
    		if (isalpha(letter)){
    			i ++;
    			while (isalpha(letter))
    			scanf("%c", &letter);
    		}
    		if (letter == '\n'){
    			printf("%d\n", i);
    			i = 0;
    		}
    	}
    	
    	return 0;
    }
       嗯,这次就这样吧。要休息一会了,不然就太累了。。。

  • 相关阅读:
    进程控制
    进程的状态与转换
    进程的概念和特征
    操作系统的体系结构:大内核和微内核
    系统调用
    中断和异常
    【转】Java-----jar反编译修改重新打包
    [转] Android Volley完全解析(一),初识Volley的基本用法
    [转] This Android SDK requires Android Developer Toolkit version 23.0.0 or above
    Can't bind to local 8700 for debugger报错和解决
  • 原文地址:https://www.cnblogs.com/java20130723/p/3212218.html
Copyright © 2011-2022 走看看