Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/32768 K (Java/Others)
[显示标签]
Description
统计给定文本文件中汉字的个数。
Input
输入文件首先包含一个整数n,表示测试实例的个数,然后是n段文本。
Output
对于每一段文本,输出其中的汉字的个数,每个测试实例的输出占一行。
[Hint:]从汉字机内码的特点考虑~
Sample Input
2
WaHaHa! WaHaHa! 今年过节不说话要说只说普通话WaHaHa! WaHaHa!
马上就要期末考试了Are you ready?
Sample Output
14
9
Hint
lcy
Source
C语言程序设计练习(五)
Related problem
2032 2031 2024 2034 2022
这道题的实现并不难,只要理解了关于汉字码的内容即可。就算不了解,只要知道汉字码是小于0的即可。并且一个汉字占两个字节。
代码如下:
#include<iostream>
#include<string.h>
using namespace std;
int main(){
int n;
cin>>n;
cin.ignore();
while(n--){
char a[1000];
cin.getline(a,1000);
int count=0;
for(int i=0;i<strlen(a);i++)
if(a[i]<0)count++;
cout<<count/2<<endl;
}
}