【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();
	}
}
相关推荐
smj2302_79682652几秒前
解决leetcode第3989题网格中保持一致的最大列数
python·算法·leetcode
临床数据科学和人工智能兴趣组10 分钟前
R语言因其强大的统计功能、灵活的编程环境、活跃的社区支持和强大的R扩展包,迅速成为统计学和数据科学领域的首选工具之一
开发语言·数据分析·r语言·r语言-4.2.1
wangbing112526 分钟前
JPA下自定义主键
java·linux·服务器
__log44 分钟前
弱网环境下的“生命线“:从AI流式响应到大文件上传的稳定性
开发语言·人工智能·php
吴梓穆1 小时前
Python 基础 正则表达式
python
巧克力男孩dd1 小时前
Python超典型练习题(第一次作业)
开发语言·python·算法
plainGeekDev1 小时前
NullPointerException → Kotlin 空安全
android·java·kotlin
TlSfoward1 小时前
TLSFOWARD TLS指纹
开发语言·数据库·爬虫·搜索引擎·https·php
闲猫2 小时前
LangChain / Core components / Models
开发语言·python·langchain
想做小南娘,发现自己是女生喵2 小时前
第 2 章 顺序表和 vector
java·数据结构·算法