zoukankan      html  css  js  c++  java
  • 练习112:编写一个程序,以每行一个单词的形式打印其输入。

    我的答案:

    #include <stdio.h>

    int main()
    {
    int c;
    while( (c = getchar()) != EOF)

    if(c == ' ' || c == '\n' || c == '\t')
    printf("\n");
    else
    putchar(c);

    }

    思路 检测到得空格 换行 制表  都用换行代替。

    答案:

    #include <stdio.h>
    #define IN 1
    #define OUT 0

    int main()
    {
    int c, state;
    state = OUT;

    while( (c = getchar()) != EOF)

    {
    if(c == ' ' || c == '\n' || c == '\t')
    {
    if(state == IN)
    {
    putchar('\n');
    state = OUT;
    }
    }else if (state == OUT)
    {
    state = IN;
    putchar(c);
    }else
    putchar(c);

    }
    }

    总结: 我的答案思路大致没错..但是少考虑了多次出现单词分隔时,替换的换行符会相继增多..这样就多出很多空的行。

    书答案的代码 对state的布尔量的理解还是不够...还需要慢慢理解.

  • 相关阅读:
    Odd sum CodeForces
    Chips CodeForces
    Secrets CodeForces
    Voting CodeForces
    Jury Meeting CodeForces
    Planning CodeForces
    Maxim Buys an Apartment CodeForces
    Chemistry in Berland CodeForces
    Monitor CodeForces
    Four Segments CodeForces
  • 原文地址:https://www.cnblogs.com/jango/p/3377686.html
Copyright © 2011-2022 走看看