洛谷 题解 P5015 【标题统计】 NOIP2018 普及组 T1

没有人用 scanf("%c", &ch) != EOF 吗? scanfEOF 会伤心的。

思路:逐个读入字符,遇到EOF中止,对于每个读入的字符进行判断。

附上考场代码:

#include <stdio.h>
#define file_in(f) freopen(f".in", "r", stdin)
#define file_out(f) freopen(f".out", "w", stdout)
#define file(f) file_in(f), file_out(f)

char tmp;

bool Daxie(char c)
{
	return (c <= 'Z') && (c >= 'A');
}
bool Xiaoxie(char c)
{
	return (c <= 'z') && (c >= 'a');
}
bool Number(char c)
{
	return (c <= '9') && (c >= '0');
}
bool isOK(char ch)
{
	return Daxie(ch) || Xiaoxie(ch) || Number(ch);
}

int main()
{
// 	file("title");
	int ans = 0;
	while (scanf("%c", &tmp) != EOF) {
		if (isOK(tmp)) ans++;
	}
	printf("%d", ans);
	return 0;
}

To: 不太理解的

scanf 如果读入不到内容则会返回 -1 , 即 EOF 。 这里因为是文件读入,所以必然在文件尾处返回 EOF

posted @ 2018-11-20 21:45  航空信奥  阅读(452)  评论(0编辑  收藏  举报