问题:
java判断中文比较复杂。
汉字统计
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 29746 Accepted Submission(s): 16314
Problem Description
统计给定文本文件中汉字的个数。
Input
输入文件首先包含一个整数n,表示测试实例的个数,然后是n段文本。
Output
对于每一段文本,输出其中的汉字的个数,每个测试实例的输出占一行。
[Hint:]从汉字机内码的特点考虑~
Sample Input
2 WaHaHa! WaHaHa! 今年过节不说话要说只说普通话WaHaHa! WaHaHa! 马上就要期末考试了Are you ready?
Sample Output
14 9
代码:
import java.util.*; public class Main{ public static void main(String args[]){ Scanner cin=new Scanner(System.in); int n=cin.nextInt(); String s; cin.nextLine(); for(int i=0;i<n;i++){ s=cin.nextLine(); char[] a=s.toCharArray(); int count=0; for(int j=0;j<s.length();j++){ Character.UnicodeBlock ub = Character.UnicodeBlock.of(a[j]); if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) count++; } System.out.println(count); } } }
时间: 2024-10-11 02:19:48