HDU-2030

汉字统计

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 20579    Accepted Submission(s): 11242

Problem Description

统计给定文本文件中汉字的个数。
 
Input
输入文件首先包含一个整数n,表示测试实例的个数,然后是n段文本。 
Output
对于每一段文本,输出其中的汉字的个数,每个测试实例的输出占一行。

[Hint:]从汉字机内码的特点考虑~
 
Sample Input
2
WaHaHa! WaHaHa! 今年过节不说话要说只说普通话WaHaHa! WaHaHa!
马上就要期末考试了Are you ready? 
Sample Output
14
9
汉字机内码:每个占用两个字节,并且汉子的机内码第一位都为1,而机内码用补码表示,也就是说,汉字机内码的真值为负数,以此判断汉字
 1 #include<stdlib.h>
 2 #include<math.h>
 3 #include<iostream>
 4 #include<string.h>
 5 #include<algorithm>
 6 using namespace std;
 7 int main()
 8 {
 9     int n;
10     char s[1000];
11     int i;
12     int count;
13     int len;
14     scanf("%d",&n);
15     getchar();
16     while (n--)
17     {
18         count = 0;
19         gets(s);
20         len=strlen(s);
21         for (i=0;i<len;i++)
22             if (s[i]<0) count++;
23         printf("%d
",count/2);
24     }
25     return 0;
26 }
原文地址:https://www.cnblogs.com/leiyuxiang/p/3495661.html