暑假做题0710(Java)

汉字统计

统计给定文本文件中汉字的个数。
Input
输入文件首先包含一个整数n,表示测试实例的个数,然后是n段文本。
Output
对于每一段文本,输出其中的汉字的个数,每个测试实例的输出占一行。

[Hint:]从汉字机内码的特点考虑~
Sample
Input	
2
WaHaHa! WaHaHa! 今年过节不说话要说只说普通话WaHaHa! WaHaHa!
马上就要期末考试了Are you ready?
Output
14
9

汉字的Ascii码是负数,一个汉字占两个字节,所以要除二

package reshen2;

import java.util.Scanner;

public class D {
    public static void main(String[] args) {
        Scanner sc= new Scanner(System.in);
        int n=Integer.parseInt(sc.nextLine());
        for(int i=0;i<n;i++){
            int count=0;
            byte[]arr=sc.nextLine().getBytes();
            for(byte c:arr){
                if(c<0){
                    count++;
                }
            }
            System.out.println(count/2);
        }
    }
}

进制转换

输入一个十进制数N,将它转换成R进制数输出。
Input
输入数据包含多个测试实例,每个测试实例包含两个整数N(32位整数)R2<=R<=16, R<>10)。
Output
为每个测试实例输出转换后的数,每个输出占一行。如果R大于10,则对应的数字规则参考16进制(比如,10A表示,等等)。
Sample
Input
7 2
23 12
-4 3
Output
111
1B
-11

除多少取余

package reshen2;

import java.util.Scanner;

public class E {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        while (sc.hasNext()){
            int[] arr=new int[500];
            int c=0;
            int n=sc.nextInt();int r=sc.nextInt();
            if(n==0) {
                System.out.println(0);
                continue;
            }
            if(n<0){
                System.out.print("-");
                n=-n;
            }

            while(n!=0){
                arr[c]=n%r;
                c++;
                n/=r;
            }
            for(int i=c-1;i>=0;i--){
                if(arr[i]>9){
                    System.out.printf("%c",'A'+arr[i]-10);
                } else {
                    System.out.print(arr[i]);
                }
            }
            System.out.println();
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

是君倩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值