[HDU] [2030] Chinese Character Statistics

Problem Description

Loading...

Problem Analysis

The principle is that the ASCII code of Chinese characters is negative.

Note that the input may contain spaces.
Also, a Chinese character occupies two characters.
So if you count by incrementing by 1 in a loop,
you need to halve the result.

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
#include<stdio.h>
#include<string.h>
int main() {
	int T;
	scanf("%d",&T);
	getchar();
	while(T--) {
		char s[100020];
		gets(s);
		int cnt=0;
		for(int i=0; i<strlen(s); i++) {
			if(s[i]<0)
				cnt++;
		}
		printf("%d\n",cnt/2);
	}
	return 0;
}

Problem link: [HDU] [2030] Chinese Character Counting

This post is licensed under CC BY-NC-SA 4.0. If it helped, or if you have anything to say, the comment box below is open.
Page views —
Unique visitors —

Loading...