📑前言
本文主要是【算法】------蓝桥杯练习题(八)的文章,如果有什么需要改进的地方还请大佬指出⛺️
🎬作者简介:大家好,我是听风与他🥇
☁️博客首页:CSDN主页听风与他
🌄每日一句:狠狠沉淀,顶峰相见
目录
2122.数位排序
java
package 蓝桥杯第八次;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
public class 数位排序{
/*
13
5
o1-o2 就是从小到达排序 负就不换,正就换
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
List<Integer> list = new ArrayList<>();
for(int i=1;i<=n;i++) {
list.add(i);
}
//对集合的一种排序
Collections.sort(list,new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
// TODO Auto-generated method stub
if(sum(o1)!=sum(o2)) {
return sum(o1)-sum(o2);
}else {
return o1-o2;
}
}
});
System.out.println(list.get(m-1));
}
public static int sum(int n) {
int ans = 0;
while(n>0) {
ans+=n%10;
n/=10;
}
return ans;
}
}
1037.子串分值和
java
package 蓝桥杯第八次;
import java.util.Arrays;
import java.util.Scanner;
public class 子串分值和1 {
/*
ababc
28
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
char a[] = sc.next().toCharArray();
int last[] = new int[26];
long res = 0;
int len = a.length;
Arrays.fill(last, -1);
for(int i=0;i<a.length;i++) {
res +=(long)(i-last[a[i]-'a'])*(len-i);
last[a[i]-'a']=i;//更新一下last数组
}
System.out.println(res);
}
}
244.最长子序列
java
package 蓝桥杯第八次;
import java.util.Scanner;
public class 最长子序列 {
/*
ABCDEABCD
AABZ
3
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
String s = sc.next();
String t = sc.next();
char a[] = s.toCharArray();
char b[] = t.toCharArray();
int i=0,j=0;
while (i!=a.length&&j!=b.length) {
if(a[i]==b[j]) {
i++;
j++;
}else {
i++;
}
}
System.out.println(j);
}
}