C++头文件
#include <iostream>
#include <cmath> //数学公式 例如绝对值abs()、幂函数pow()...
#include <string> //C++字符串
#include <string.h> //C语言库,对应C++是cstring
#include <algorithm> //常用函数,max()、min()、abs()、swap()、sort()...
num.sort()
#include <bits/stdc++.h> //万能头文件
using namespace std;
int main(){
//please write your code!
return 0;
}
getchar()//输入一个字符
getline()//输入一行带空格的字符串
cin //相当于next()
string 下标0
vector<int> v;//数组
//排序
#include<bits/stdc++.h>
using namespace std;
bool cmp(int a,int b){
return a < b;
}
int main(){
vector<int> v;
int id;
for(int i = 0; i < 5; i++){
cin >> id;
v.push_back(id);
}
sort(v.begin(),v.end(),cmp);
for(int i = 0; i < 5; i++){
cout << v[i] << " ";
}
return 0;
}
JAVA快速输入
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main{
static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer tokenizer = new StringTokenizer("");
public static void main(String[] args) throws IOException{
}
static String next() throws IOException {
while (!tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
static String nextLine() throws IOException {
return reader.readLine();
}
static int nextInt() throws IOException {
return Integer.parseInt(next());
}
static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
}
JAVA输入多组数据
每次都是line在读取输入流中的数据,若是line等于啥就跳出循环,就在开头写if-break……
public static void method() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = null;
while((line=br.readLine())!=null) {
//其他操作
}
}
变量类型转换
1、int String
String转换为int型
//convert str(String) to i(int)
String str;
int i = Integer.parseInt(str);
int型转换为String
//conver i(int) to str(String)
int i;
String str = i + "";
Compare()函数
实现Comparator接口并重写compara方法,在compara方法中,**如果返回值是负数,就是升序排序;如果返回值是正数,就是降序排序;**如果是0,就是两个一样大。
//数组排序
String[] str = new String[5];
Arrays.sort(persons, new Comparator<Person>() {
@Override
public int compare(Person o1, Person o2) {
if(o1.sum == o2.sum) {
return o1.avg - o2.avg > 0 ? 1:-1;//从小到大
}
return (int)(o2.sum - o1.sum);//从大到小
}
});
//List集合排序
ArrayList<String> list = new ArrayList<String>();
Collections.sort(list, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
// TODO Auto-generated method stub
return 0;
}
});
图
DFS:先是外层有一个for循环,if(visit[i])
查探哪个点未被访问,在内部再写一个for循环,往深度查找,未被访问的点输出,访问过的点略过。
BFS:先是外层有一个for循环,if(visit[i])
查探哪个点未被访问,在内部再写一个while循环,每层打印,并找到对应下一层未遍历到的结点,加入队列中且修改为已遍历过,队列再次pollFirst(),找它的下层结点放入队列中……
求得无向图的最短路径需要的东西:
- 起始点
- 记录边的权值的二维数组
- 记录出发点到每个点的最短距离的数组
- 记录每个点构成目前最短路径的前一个点的数组
- 判断当前点是否走过的数组
- 如果要求最短路径的条数,类似于机器人那道题,记录每个点有几条路可以到的数组
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main{
static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer tokenizer = new StringTokenizer("");
public static void main(String[] args) throws IOException{
n = nextInt();
int m = nextInt();
num = new int[n+1][n+1];//城市之间的连接
for(int i = 0; i < m; i++) {
int a = nextInt();
int b = nextInt();
int c = nextInt();
num[a][b] = c;
num[b][a] = c;
}
start = nextInt();
end = nextInt();
getShortestPaths();
System.out.println(minlen[end]);
printfPath(end);
}
static int n; //节点数
static int start; //出发点
static int end;//结束点
static int[][] num;//权值
//以上需要输入提供
static int[] minlen; //出发点到每个点的最短路径
static int[] before; //前一个点
static boolean[] judge;//判断是否走过
static public void getShortestPaths() {
before = new int[n+1];
Arrays.fill(before, Integer.MAX_VALUE);
minlen = new int[n+1];
Arrays.fill(minlen, Integer.MAX_VALUE);
judge = new boolean[n+1];
before[start] = start;//开始点的前一个点设置为自己
minlen[start] = 0;//起始点到起始点的距离为0
int m = n;
int now = start;
while(m-- > 0) {
judge[now] = true;
for(int i = 1; i <= n; i++) {
if(num[now][i] != 0 && !judge[i] && num[now][i]+minlen[now] < minlen[i]){
before[i] = now;
minlen[i] = num[now][i] + minlen[now];
//当前点到连通点的距离+当前点到起始点的长度 = 起始点到连通点距离
}
}
int findMin = Integer.MAX_VALUE;
for(int i = 1; i <= n; i++) {
if(minlen[i] < findMin && !judge[i]){
now = i;
findMin = minlen[i];//在起始点到任意点,找最小的那个路径继续下去
}
}
}
return;
}
//倒序输出before记录的前节点
static void printfPath(int x){
if(x == before[x]) {
System.out.print(x);
return;
}
printfPath(before[x]);
System.out.print("->"+x);
return;
}
}
英文大小写转换
- str.toLowerCase(); //转换成小写
- str.toUpperCase(); //转换成大写
System.out.printf("%04d - %04d = %04d\n", m, n, num);
System.out.println(String.format("%.2f",t));
并查集
public static int find(int x) {
if(num[x] != x) {
num[x] = find(num[x]);
}
return num[x];
}
//a当父,找a的老大给b的老大
num[find(b)] = find(a);
二叉树某序列变层序
后续变层序
层序就是一个数组,是哪个位置的就把 num[x] = temp[m++]
放在哪,其他依次是x*2, x*2+1
public static void main(String[] args) throws IOException {
n = nextInt();
num = new int[32];
temp = new int[32];
for(int i = 0; i < n; i++){
temp[i] = nextInt();
}
Func(1);
for(int i = 1; i <= n; i++) {
System.out.print(num[i]);
if(i != n) {
System.out.print(" ");
}
}
}
static int[] num;
static int[] temp;
static int n, m = 0;
static void Func(int x) {
if(x > n){
return;
}
Func(x << 1);
Func(x << 1 | 1);
/* Func(x*2);
Func(x*2+1);*/
num[x] = temp[m++];
}
二叉搜索树构建完全二叉树,并层序遍历
static int[] a;//存节点数值,构建二叉搜索树
static int[] b;//数组存储,直接按照层序来存储
static int length = 0;
static void create(int t) {
if(t >= b.length) {
return;
}
create(t*2);
b[t] = a[length++];
create(t*2 + 1);
}
StringBuilder类
一个可变的字符序列。此类提供一个与 StringBuffer
兼容的 API,但不保证同步。该类被设计用作 StringBuffer
的一个简易替换,用在字符串缓冲区被单个线程使用的时候(这种情况很普遍)。每个方法都能有效地将给定的数据转换成字符串,然后将该字符串的字符添加或插入到字符串生成器中。
构造方法
字符串生成器,初始容量为 16 个字符。
StringBuilder stringbuilder = new StringBuiler();
StringBuilder stringbuilder = new StringBuiler("~~~");
//用于字符串处理
函数方法 | 用处 | |
---|---|---|
append | 拼接字符、字符串、字符数组 | |
append(CharSequence s, int start, int end) | 将s的start和end的内容拼接 | |
charAt(int index) | 获取指定字符 | |
[delete](…/…/java/lang/StringBuilder.html#delete(int, int))(int start, int end) | 删除一段字符串 | |
deleteCharAt(int index) | 删除一个指定的字符 | |
indexOf(String str) | 返回此字符串中指定的子字符串的第一个匹配项的索引。 | |
insert(int index, String str) | 将参数字符串插入指定的索引位置 | |
[replace](…/…/java/lang/StringBuilder.html#replace(int, int, java.lang.String))(int start, int end, String str) | 使该字符序列替换为该序列的反转。 | |
reverse() | 使该字符序列替换为该序列的反转。 | |
substring(int start, int end) | 取当前字符串两个索引所取的子字符串 |
java字符串反转
public static String reverseTestOne(String s) {
return new StringBuffer(s).reverse().toString();
}
Math
- abs 绝对值
- gcd 最大公约数
- pow 次方
- sqrt 开方
split
String[] strs = str.split("\\s+");
//去掉字符串内多余的空格,并切割字符串
String regex = "-";
String[] strArr = age.split(regex);//分割成字符串数组
String str="czh!han.bb test@love";
//用'!','.',空格三个,'@'来分割字符串
String[] a=str.split("\\s+|\\.|\\!|@");//竖线是或者的意思~
System.out.println(Arrays.toString(a));
String[] s = str.split(",|\\.");//按照标点切割
String regex = "\\d";
String ss = "*";
String result = s.replaceAll(regex,ss);
//10进制转换 2进制
System.out.println(Integer.toBinaryString(val));
//16进制转换 10进制
System.out.println(Integer.valueOf("f", 16));
//8进制转换 10进制
System.out.println(Integer.valueOf("11", 8));
//2进制转换 10进制
System.out.println(Integer.valueOf("0101", 2));
01背包
// w 容量 n 物品数 wight 体积数组 value 价值数组
public static int[][] BackPack(int w, int n, int[] wight, int[] value){
int[][] note = new int[n+1][w+1];//最大价值数组
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= w; j++) {
if(j >= wight[i]) {//当前容量不装其他的,可以装当前i商品
note[i][j] = Math.max(note[i-1][j], note[i-1][j-wight[i]] + value[i]);
}else{//肯定不能装i商品,沿用上一层同容量
note[i][j] = note[i-1][j];
}
}
}
return note;
}