HDU - 2030 汉字统计

Description

统计给定文本文件中汉字的个数。

Input

输入文件首先包含一个整数n,表示测试实例的个数,然后是n段文本。

Output

对于每一段文本,输出其中的汉字的个数,每个测试实例的输出占一行。

[Hint:]从汉字机内码的特点考虑~
 

Sample Input

2
WaHaHa! WaHaHa! 今年过节不说话要说只说普通话WaHaHa! WaHaHa!
马上就要期末考试了Are you ready?

Sample Output

14
9

【解析】:汉字的机内码使用2个字节,每个汉字的十进制都是负数。因此逐个存取遍历出小于0的字符除以2即得到答案。 

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

char s[1000];

int main()
{
    int n, len, count;

    scanf("%d", &n);
    getchar();

    while (n--)
    {
        count = 0;
        gets(s);
        len = strlen(s);
        for (int i = 0; i < len; i++)
        {
            if (s[i] < 0)
                count++;
        }
        printf("%d\n", count/2);
    }
    return 0;
}
发布了329 篇原创文章 · 获赞 342 · 访问量 17万+

猜你喜欢

转载自blog.csdn.net/Aibiabcheng/article/details/105320246