这个网上方案是汉字内码,因为最高位为1所以是负数,而且占两个字节。所以用java写就是把string转为字节数组,求出负数再除以2.
还有一种是转为字符数组,大于最大的128就是汉字。
import java.util.Scanner;
public class P2030 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();scanner.nextLine();
while (n-->0){
String string = scanner.nextLine();
char [] ch = string.toCharArray();
int count =0;
for(int i=0;i
int m =ch[i];
if(m>128)
count++;
}
System.out.println(count);
}
}
}