题干:
统计给定文本文件中汉字的个数。
Input
输入文件首先包含一个整数n,表示测试实例的个数,然后是n段文本。
Output
对于每一段文本,输出其中的汉字的个数,每个测试实例的输出占一行。
[Hint:]从汉字机内码的特点考虑~
Sample Input
2 WaHaHa! WaHaHa! 今年过节不说话要说只说普通话WaHaHa! WaHaHa! 马上就要期末考试了Are you ready?
Sample Output
14 9
解题报告:
普及一下,汉字的ASCII码都是负值,并且占两个字符。所以其实不要循环体内的i++并且输出ans/2也是对的。
顺便,ASCII码为127的是delete键、、、所以输出不来。
AC代码:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int MAX = 1e6 +5;
char s[MAX];
ll ans;
int main()
{int t;cin>>t;getchar();while(t--) {gets(s+1);ans = 0;for(int i = 1; i<strlen(s+1); i++) {if(s[i] >= 0 && s[i] <= 127) continue;ans++;i++;}printf("%lld\n",ans);}return 0 ;}