【Java-简单练习题】

1.”AABBBCCC“>>"A2B3C3"

public class Test6 {
	public static void main(String[] args) {
		String ns = "AABBBCCCC";
		String ret=compress(ns);
		System.out.println(ret);
	}
	
public static String compress(String str) {
	StringBuilder ret = new StringBuilder();
	int count = 1;
	// 遍历至倒数第二个
	for (int i = 0; i < str.length()-1; i++) {
		if (str.charAt(i) == str.charAt(i + 1)) {
			count++;
		} else {
			ret.append(str.charAt(i)+""+count);
			count=1;
		}
	}
	//处理最后一个字符
	ret.append(str.charAt(str.length()-1)+""+count);
	return ret.toString();
}
}

2. 图书ISBN验证码

package ti;
import java.util.Scanner;
public class Test02 {
	 public static void main(String[] args) {
	        Scanner scan = new Scanner(System.in);
	        //在此输入您的代码...
	        //输入ISBN
	        String isbn=scan.next();
	        //去除分隔符
	        String isbn1=isbn.replace("-","");
	        //累加
	        int sum=0;
	        for(int i=0,k=1;i<isbn1.length()-1;i++,k++){
	            int na=Integer.parseInt(String.valueOf(isbn1.charAt(i)));
	            sum=sum+na*k;
	        }
	        //计算校验码
	        String code=String.valueOf(sum%11==10?"X":sum%11);
	        //判断
	        String isbncode=String.valueOf(isbn.charAt(isbn.length()-1));
	        if(code.equals(isbncode)){
	          System.out.println("Right");
	        }else{
	        	 System.out.println(isbn.substring(0,isbn.length()-1)+code);
	        }
	        scan.close();
	    }}

3. 统计字母数字中文符号的个数

public static void main(String[] args) {
		String str = "OMG,你们的中英混搭真是各有千秋,666但Someone丝毫掩盖不了你们那硬朗的英语底子!For eg.papi酱真的very有才华啊";
		HashMap<String, Integer> map = new HashMap<String, Integer>();
		map.put("letters", 0);
		//map.put("numbers", 0);
		map.put("chinese", 0);
		map.put("flags", 0);
		// 遍历字符串
		for (int i = 0; i < str.length(); i++) {
			char c = str.charAt(i);
			if (c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z') {
				int oldValue = map.get("letters");
				int newValue = oldValue + 1;
				map.put("letters", newValue);
			}if(c >= '0' && c <= '9') {
				map.put("numbers", map.getOrDefault("numbers",0)+1);
			}if(c >= 0x4e00 && c <= 0x29fa5) {
				map.put("chinese", map.get("chinese")+1);
			}else {
				map.put("flags", map.get("flags")+1);
			}
		}
		System.out.println(map);
	}

4. 幸运数字

package learn01;

import java.math.BigInteger;

public class Test02 {
	public static void main(String[] args) {
		int i = 1, counter = 0;
		while (true) {
			String bin = Integer.toBinaryString(i);
			String oct = Integer.toOctalString(i);
			String dec = String.valueOf(i);
			String hex = Integer.toHexString(i);

			int binsum = sum(bin, 2);
			int octsum = sum(oct, 8);
			int decsum = sum(dec, 10);
			int hexsum = sum(hex, 16);

			if (i % binsum == 0 && i % octsum == 0 && i % decsum == 0 && i % hexsum == 0) {
				System.out.println(i);
				counter++;
			}
			if(counter>=2023) {
				break;
			}
			i++;
		}
	}
	
	private static int sum(String number,int radix) {
		BigInteger ret=new BigInteger("0",radix);
		
		for(int i=0;i<number.length();i++) {
			BigInteger bn=new BigInteger(number.substring(i,i+1),radix);
			ret=ret.add(bn);
		}
		return ret.intValue();
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值