【Java-简单练习题】

1."AABBBCCC">>"A2B3C3"

java 复制代码
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();
}
}
  1. 图书ISBN验证码
java 复制代码
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();
	    }}
  1. 统计字母数字中文符号的个数
java 复制代码
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);
	}
  1. 幸运数字
java 复制代码
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();
	}
}
相关推荐
心愿许得无限大3 分钟前
Qt 常用界面组件
开发语言·c++·qt
2401_8582861113 分钟前
OS15.【Linux】gdb调试器的简单使用
linux·运维·服务器·开发语言·gdb
牛马baby14 分钟前
MATLAB下载安装教程(附安装包)2025最新版(MATLAB R2024b)
开发语言·matlab
小龙在山东19 分钟前
Python 包管理工具 uv
windows·python·uv
shenyan~23 分钟前
关于 c、c#、c++ 三者区别
开发语言·c++
Ashlee_code34 分钟前
什么是Web3?金融解决方案
开发语言·金融·架构·eclipse·web3·区块链·php
Edingbrugh.南空38 分钟前
Flink ClickHouse 连接器数据读取源码深度解析
java·clickhouse·flink
weixin_3077791341 分钟前
批量OCR的GitHub项目
python·github·ocr
Evand J1 小时前
【MATLAB例程】AOA与TDOA混合定位例程,适用于三维环境、4个锚点的情况,附下载链接
开发语言·matlab
机器视觉知识推荐、就业指导1 小时前
Qt 与Halcon联合开发八: 结合Qt与Halcon实现海康相机采图显示(附源码)
开发语言·数码相机·qt