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