importjava.util.Scanner;//输入Scanner sc =newScanner(System.in);int n=sc.nextInt();double d=sc.nextDouble();String str=sc.next();char c=sc.next().charAt(0);while(sc.hasNextInt()){int n =sc.nextInt();System.out.println(n);}// Ctrl+D 停止//输出System.out.println();System.out.println(String.format("%04d",4));//0004(输出规范)System.out.println(String.format("%04d",11));//0011double a =1.2349;String a =String.format("%.2f",a);//1.23
import java.math.BigDecimal;
import java.math.BigInteger;
//整数
BigInteger a=BigInteger.ONE;
String s1 = sc.next(); //150
String s2 = "2";
BigInteger a = new BigInteger(s1);
BigInteger b = new BigInteger(s2);
System.out.println(a); //150
System.out.println(a.multiply(b));//相乘 300
System.out.println(a.add(b));//相加 152
System.out.println(a.subtract(b));//相减 148
System.out.println(a.divide(b));//相除 75
//小数
BigDecimal a,b;//同上
int intMax = Integer.MAX_VALUE; // 2147483647
long longMax = Long.MAX_VALUE; // 9223372036854775807
abs(Object o);
min(Object a, Object b)
max(Object a, Object b)
pow(double a, double b)
sqrt(double a)
round(float a)
ArrayList<ArrayList<Integer>> mmp = new ArrayList<>(); // 双层数组
// java中只是预留空间,并不会初始化100个String
ArrayList<String> list = new ArrayList<>(100);
for (int i = 0; i < 100; i++) list.add("hello"); // 添加
list.set(0, "world"); // 修改
System.out.println(list.get(0)); // 获取
list.remove(0); // 移除
// 相互转换
Integer[] array = new Integer[]{1,2,3,4,5};
ArrayList<Integer> arrayList = new ArrayList<>(Arrays.asList(array));
Integer[] array2 = arrayList.toArray(new Integer[0]);
import java.util.ArrayList;
ArrayList<Integer> a = new ArrayList<>(100);
a.add(1);a.add(2);a.add(4);
a.add(5);a.add(3);a.add(7);
a.sort((a, b) -> {
return b - a;
// 如果结果小于0,a优先级大,放前面,在这里就是大的放前面
});
for (int i : a) System.out.println(i);
class Student {
public String name;
public int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
}
ArrayList<Student> list = new ArrayList<>();
list.add(new Student("Tom", 20));list.add(new Student("Jerry", 18));
list.add(new Student("Alice", 22));list.add(new Student("Bob", 19));
list.sort(new Comparator<Student>() {
public int compare(Student a, Student b) {
return a.age - b.age; // 按年龄升序排列
}
});
for (Student s : list) System.out.println(s.name + " " + s.age);
ArrayList<Integer> list = new ArrayList<>();
list.add(30);list.add(40);list.add(10);
list.add(20);list.add(50);
list.sort((x,y)->{
return x-y;
});
// 使用binarySearch方法查找元素
int index = Collections.binarySearch(list, 30);
// 输出查找结果,索引位置从0开始
if (index >= 0) System.out.println("元素30在列表中的索引位置为:" + index);
else System.out.println("元素30不在列表中");
HashSet<Integer> s = new HashSet<>();
boolean result = s.add(12); // 返回一个boolean值,表示是否插入成功
System.out.println(s.size());
boolean found = s.contains(12); // 判断集合中是否包含元素12
System.out.println(found ? 12 : "Not found!");
s.remove(12); // 移除元素12
found = s.contains(12);
System.out.println(found ? 12 : "Not found!");
s.add(123);s.add(2);s.add(1322);
for(int num : s) System.out.print(String.format("%d ", num));
HashMap<String, String> m = new HashMap<>();
m.put("one", "yi"); // 插入键值对 "one" -> "yi"
System.out.println(m.get("one")); // 输出 yi
m.put("one", "1"); // 修改键值对 "one" -> "1"
System.out.println(m.get("one")); // 输出 1
// 查找键 "two"
if (m.containsKey("two")) System.out.println(m.get("two"));
m.remove("one"); // 移除
import java.util.PriorityQueue;
PriorityQueue<Integer> pq = new PriorityQueue<Integer>((a,b) -> b - a); // 大的在前面
pq.add(1);pq.add(2);pq.add(3);
while(!pq.isEmpty()) System.out.println(pq.poll());
int[] res = new int[nums.length - k + 1];
Deque<Integer> deque = new LinkedList<>();
for (int i = 0; i < nums.length; i++) {
if (!deque.isEmpty() && deque.peekFirst() < i - k + 1) {
deque.pollFirst();
}
while (!deque.isEmpty() && nums[deque.peekLast()] < nums[i]) {
deque.pollLast();
}
deque.offerLast(i);
if (i >= k - 1) {
res[i - k + 1] = nums[deque.peekFirst()];
}
}
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
ArrayList<Character> mmp = new ArrayList<>();
ArrayList<Character> ans = new ArrayList<>();
for(int i = 0; i < 10; i++) mmp.add((char)(i + '0'));
for(char i = 'A'; i <= 'F'; i++) mmp.add(i);
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
if(num==0) {
System.out.print(0);
return;
}
while(num!=0) {
ans.add(mmp.get(num%16));
num/=16;
}
for(int i=ans.size()-1; i>=0; --i) System.out.print(ans.get(i));
}
}
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
static int[] fa = new int[5005];
static int getfa(int x) {
if(fa[x]==x) return x;
return fa[x]=getfa(fa[x]);
}
static void setfa(int son, int father) {
fa[getfa(son)]=getfa(father);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int M = sc.nextInt();
int P = sc.nextInt();
for(int i = 0; i <= N; ++i) fa[i]=i;
for(int i = 0; i < M; ++i) {
int num1 = sc.nextInt();
int num2 = sc.nextInt();
setfa(Math.min(num1, num2), Math.max(num1, num2));
}
for(int i = 0; i < P; ++i) {
int num1 = sc.nextInt();
int num2 = sc.nextInt();
if(getfa(num1)==getfa(num2)) System.out.println("Yes");
else System.out.println("No");
}
}
}
public class T11 {
public static void main(String[] args) {
Tree root = new Tree(1);
root.left = new Tree(2);
root.right = new Tree(3);
root.left.left = new Tree(4);
root.left.right = new Tree(5);
root.right.left = new Tree(6);
root.right.right = new Tree(7);
root.preOrder(root);
}
}
class Tree{
public int val;
public Tree left;
public Tree right;
public Tree(int val){
this.val = val;
}
void preOrder(Tree root){
if(root == null){
return;
}
System.out.println(root.val);
preOrder(root.left);
preOrder(root.right);
}
}
import java.util.PriorityQueue;
import java.util.Comparator;
import java.util.Scanner;
public class T10 {
public static int MAX = 10003;
public static int[] dis = new int[MAX];
public static int[] vis = new int[MAX];
public static int[] head = new int[MAX];
public static Edge[] edge = new Edge[MAX*2];
public static int cnt = 0;
public static void addEdge(int u, int v, int w){
edge[++cnt] = new Edge(v, w);
edge[cnt].next = head[u];
head[u] = cnt;
}
public static void dijstra(int s){
for (int i = 0; i < MAX; i++) {
dis[i] = Integer.MAX_VALUE;
vis[i] = 0;
}
dis[s] = 0;
PriorityQueue<Node> q = new PriorityQueue<>(new NodeComp());
q.add(new Node(s, 0));
while (!q.isEmpty()) {
Node cur = q.poll();
int u = cur.pos;
if (vis[u] == 1) {
continue;
}
vis[u] = 1;
for (int i = head[u]; i != 0; i = edge[i].next) {
int v = edge[i].to;
int w = edge[i].dis;
if (dis[v] > dis[u] + w) {
dis[v] = dis[u] + w;
q.add(new Node(v, dis[v]));
}
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N, M, u, v, w;
N = sc.nextInt();
M = sc.nextInt();
for (int i = 0; i < M; i++) {
u = sc.nextInt();
v = sc.nextInt();
w = sc.nextInt();
addEdge(u, v, w);
addEdge(v, u, w);
}
dijstra(1);
for (int i = 1; i <= N; i++) {
System.out.println(dis[i]);
}
}
}
class Node{
public int pos;
public int dis;
public Node(int pos, int dis) {
this.pos = pos;
this.dis = dis;
}
}
class NodeComp implements Comparator<Node> {
@Override
public int compare(Node a, Node b) {
return a.dis - b.dis; // 按距离升序排列
}
}
class Edge{
public int to;
public int dis;
public int next;
public Edge(int to, int dis) {
this.to = to;
this.dis = dis;
}
}