HDU2030 java实现统计给定文本文件中汉字的个数

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);
        }

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值