Comments (3)
因为在20行
https://github.com/OliverLew/PAT/blob/4ae61bab4a0a4a879da09f196226b0b6be054b6e/PATBasic/1003.c#L19-L21
如果出现不是PAT
中的任何一个字母,就立即跳出循环了,这一行剩下的内容还在输入流中,你所问的代码就是用来清理剩下的内容。
from pat.
把读操作放在一起,确实可能更直观一些,我最开始是将这句while放在break前面的,其实都大同小异,或许我应该把代码内的注释写的更清楚点
from pat.
谢谢解答,按照你的理解可以把上述代码修改,好像更容易理解。你觉得呢?
#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)
- '{{ }}' in code is treated as Liquid tags by jekyll
- PAT Basic 1068: 判断逻辑错误
- 1090PATbasic部分正确 HOT 1
- 1009.c改进意见 HOT 1
- 关于PAT Basic Level 1008 HOT 1
- 乙级1052.数组下标错误 HOT 1
- 1010代码提交,部分正确 HOT 4
- pat题解网站打不开了 HOT 2
- 1052. 卖个萌 (20) HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pat.