汉字统计
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 36676 Accepted Submission(s): 19952
Problem Description
统计给定文本文件中汉字的个数。
Input
输入文件首先包含一个整数n,表示测试实例的个数,然后是n段文本。
Output
对于每一段文本,输出其中的汉字的个数,每个测试实例的输出占一行。
[Hint:]从汉字机内码的特点考虑~
[Hint:]从汉字机内码的特点考虑~
Sample Input
2 WaHaHa! WaHaHa! 今年过节不说话要说只说普通话WaHaHa! WaHaHa! 马上就要期末考试了Are you ready?
Sample Output
14 9
题目大意:
中文题目,意思很好懂就是找中文,看有多少个中文;
思路:这里的话我们需要从汉字的内码考虑,提示有给。我们知道汉字的内码其实就是汉字的ASCll码,百度百科脑补下http://baike.baidu.com/link?url=DZPViHkBv207mVOzoivtC9OR4CT8g9c78ouSxEWyUx_8VZB8tGxVAHPz6Lec38rPmnNS-V86SK2LWC-VAQg3ZFg9QQWuJzmUtV30gPAIc86Kwx2j_nyVA3GCoAgrt01p2EyjKbEdwgrYaOUOAyo23e9oTrDjQG6RhxuwcSMBg8sMxlqRbnRe5SFfaexlKuTn
所以要找比汉字我们只要找其ascll码的值小于0的就好了,然后将其个数对半;
给出AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main()
{
int n;
int i, j;
char str[1050];
int num;
int len;
while (scanf("%d", &n) != EOF)
{
gets(str);
while (n--)
{
num = 0;
gets(str);
len = strlen(str);
for (i = 0; i<len; i++)
if (str[i]<0)
num++;
printf("%d\n", num / 2);
}
}
return 0;
}