Input
输入文件首先包含一个整数n,表示测试实例的个数,然后是n段文本。
Output
对于每一段文本,输出其中的汉字的个数,每个测试实例的输出占一行。
[Hint:]从汉字机内码的特点考虑~
Sample Input
2
WaHaHa! WaHaHa! 今年过节不说话要说只说普通话WaHaHa! WaHaHa!
马上就要期末考试了Are you ready?
Sample Output
14
9
解题思路:
1.汉字内码在计算机中是双字节存储,java中 char 可以存储两个字节,c中char只能存储一个字节。
2.在计算机中,汉字使用二个字节,每个字节最高位一位为1。 计算机中,补码第一位是符号位,1 表示为 负数。所以 汉字机内码的每个字节表示的十进制数都是负数。英文的一个字一个字节用了8位(1个字节),汉字的一个字两个字节用了16位(2个字节)。
java代码如下:
import java.util.Scanner;
public class Main {
public void run() {
Scanner sc = new Scanner(System.in);
int count = sc.nextInt();
sc.nextLine();
String str;
int result = 0;
int[] array = new int[count];
// String reg = "[\\u4e00-\\u9fa5]";
String reg = "^[\u4E00-\u9FA5]{0,}$";
// Pattern p = Pattern.compile(reg);
//Matcher m;
char[] arr;
for (int i = 0; i < count; i++) {
str = sc.nextLine();
arr = str.toCharArray();
// Matcher m = p.matcher(str);
for(int j=0;j<arr.length;j++) {
if(arr[j]<0||arr[j]>255){
result++;
}
}
//System.out.println(result);
array[i] = result;
result = 0;
}
for (int i = 0; i < count; i++) {
System.out.println(array[i]);
}
}
public static void main(String[] args) {
new Main().run();
}
}
输出如下:
3
327 没有什么能够阻挡 我对 shasjw自由的向往
中华287hjsh 好儿 女hwdh !%
!蓝莲花 为 we are 许嵩 family
15
5
6
另附代码:
装换为字节数组
import java.util.Scanner;
public class Main1{
public static void main(String[] args) {
Scanner it = new Scanner(System.in);
int n=it.nextInt();
it.nextLine();
while(it.hasNext()) {
if(n==0)break;n--;
String s=it.nextLine();
int sum=0;
byte a[]=s.getBytes();
for(int i=0;i<a.length;i++) {
if(a[i]<0)sum++;
}
System.out.println(sum/2);
}
}
}
汉字内码范围:
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner it = new Scanner(System.in);
int n =it.nextInt();
it.nextLine();
while(n-->0) {
String s = it.nextLine();
char a[]=s.toCharArray();
int sum=0;
for(int i=0;i<a.length;i++){
if(a[i] >= 0x0391 && a[i] <= 0xFFE5)
sum++;
}
System.out.println(sum);
}
}
}