4.3w+
社区成员
英文字母
import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
//在此输入您的代码...
int n = scan.nextInt();
System.out.print((char) (n - 1 + 'A'));
scan.close();
}
}
单词分析
import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
//在此输入您的代码...
String word = scan.next();
int[] arr = new int[26];
int max = 0;
for(int i = 0; i < word.length(); i++) {
int idx = word.charAt(i) - 'a';
arr[idx]++;
max = Math.max(max, arr[idx]);
}
for(int i = 0; i < 26; i++) {
if(arr[i] == max) {
System.out.println((char) (i + 'a'));
System.out.println(arr[i]);
break;
}
}
scan.close();
}
}
推导部分和
看题解摸爬滚打的写
import java.util.*;
// 1:无需package
// 2: 类名必须Main, 不可修改
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int m = scan.nextInt();
int q = scan.nextInt();
UnionFind uf = new UnionFind(n);
for(int i = 0; i < m; i++) {
int l = scan.nextInt() - 1;
int r = scan.nextInt();
long s = scan.nextLong();
uf.union(l, r, s);
}
for(int i = 0; i < q; i++) {
int l = scan.nextInt() - 1;
int r = scan.nextInt();
int rootL = uf.find(l);
int rootR = uf.find(r);
// 同一集合,就能计算出结果
if(rootL == rootR) {
System.out.println(uf.value[l] - uf.value[r]);
} else {
System.out.println("UNKNOWN");
}
}
scan.close();
}
// 带权并查集
static class UnionFind {
int[] root; // 存储每个节点的根节点,即 2-0-1, 1的根节点为2
long[] value; // 表示i到其根节点的路径长度
public UnionFind(int n) {
root = new int[n + 1];
value = new long[n + 1];
for(int i = 0; i <= n; i++) {
root[i] = i;
}
}
public int find(int x) {
if(x != root[x]) {
int tmp = root[x];
// 路径压缩优化,如 2-1-0, 本来0的父节点是1, 经过路径压缩优化后,父节点则变成根节点2
root[x] = find(root[x]); // 寻找根节点
value[x] += value[tmp];
}
return root[x];
}
public void union(int x, int y, long s) {
int rootX = find(x);
int rootY = find(y);
if(rootX != rootY) {
// 合并l和r的集合,并更新权值
// 合并规则,将小根节点集合指向大根节点集合
int small = rootX > rootY ? rootY : rootX;
int big = small == rootX ? rootY : rootX;
root[small] = big;
// 向量知识
value[small] = Math.abs(value[y] - value[x] + s);
}
}
}
}