Giter VIP home page Giter VIP logo

Comments (3)

xlucn avatar xlucn commented on July 24, 2024 2

因为在20行
https://github.com/OliverLew/PAT/blob/4ae61bab4a0a4a879da09f196226b0b6be054b6e/PATBasic/1003.c#L19-L21
如果出现不是PAT中的任何一个字母,就立即跳出循环了,这一行剩下的内容还在输入流中,你所问的代码就是用来清理剩下的内容。

from pat.

xlucn avatar xlucn commented on July 24, 2024 1

把读操作放在一起,确实可能更直观一些,我最开始是将这句while放在break前面的,其实都大同小异,或许我应该把代码内的注释写的更清楚点

from pat.

Larry2019 avatar Larry2019 commented on July 24, 2024

谢谢解答,按照你的理解可以把上述代码修改,好像更容易理解。你觉得呢?

#include <stdio.h>

int main()
{
    char c;
    int num;
    scanf("%d", &num);
    while(getchar() != '\n');  /* read the rest of the line,make sure to start from a new line later吸收回车 */
    for(int i = 0; i < num; i++)
    {
        /* pos: indicate the position of current index for count[]     */
        /* count[3]: number of As; before P, between P & T and after T */
        int pos = 0, count[3] = {0, 0, 0};
        while((c = getchar()) != '\n')
        {
            if(c == 'A')                  count[pos]++; /* count 'A's     */
            else if(c == 'P' && pos == 0) pos = 1;      /* one P before T */
            else if(c == 'T' && pos == 1) pos = 2;      /* one T after P  */
            else                          break;        /* 'wrong' string */
        }

        if(c != '\n')
            while(getchar() != '\n');//wrong string输入下,确保一次输入结束,并吸收换行符
        if(c == '\n'                        /* 1. no other characters at end */
        && pos == 2                         /* 2. appearance of 'P' and 'T'  */
        && count[1]                         /* 3. existance of 'A'           */
        && count[2] == count[1] * count[0]) /* 4. relation between numbers   */
            puts("YES");
        else
            puts("NO");

    }

    return 0;
}

from pat.

Related Issues (10)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.