刷题杂谈

一些常用的eclipse的快捷键

ctrl+shift+o 快速导包

ctrl+alt+down 快速把当前行复制到下一行

常用代码

交叉排序

关于Comparator

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Arrays.sort(a, l1, r1 + 1);

Arrays.sort(a, l2, r2 + 1, new Comparator<Integer>() {

@Override
public int compare(Integer o1, Integer o2) {
// TODO Auto-generated method stub
return o2-o1;
}
//实现数组倒序排序
});

Arrays.sort(a, l2, r2 + 1,(o1, o2) -> {return o2 - o1;})
//替换为lambda表达式

Collections.sort(list, new Comparator<Dog>() {

@Override
public int compare(Dog o1, Dog o2) {
return o1.name.compareTo(o2.name);
/* 如果参数字符串等于此字符串,则返回值 0;
如果此字符串小于字符串参数,则返回一个小于 0 的值;
如果此字符串大于字符串参数,则返回一个大于 0 的值。
*/
}
});
System.out.println("给狗狗按名字字母顺序排序:"+list);
}

//上面也可以替换为lambda表达式

最大公约数

1
2
3
4
static int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}

获取所有因数的算法

1
2
3
4
5
6
7
8
9
10
for(long i=1;i<=Math.sqrt(n);i++){
//通过求一半,减少复杂度
if(n%i == 0){
a1.add(i);
if(n != i*i){
//这里其实也可以不要,但是最好写上
a1.add(n/i);
}
}
}

excel算日期

1
2
3
4
=DATEDIF(A1,A2,"d") //日期之差
=DATEDIF(A1,A2,"m") //月份之差
=DATEDIF(A1,A2,"y") //年之差
=A1-30 日期加减

斐波拉契

1
2
3
4
5
6
7
static int Fbo(long n) {
if(n==1||n==2) {
return 1;
}else {
return Fbo(n-1)+Fbo(n-2);
}
}

斐波拉契公约数

1
gcd(f(n),f(m)) = f(gcd(n,m))

使用hasNextInt

1
2
3
4
5
6
7
8
9
10
11
while(true) {

if(sc.hasNextInt()) {
int countA = sc.nextInt();

break;
}else {
sc.next();
}

}//如果需要hasNextInt用来判断需要 .next 用来吃缓存

字符相关

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
char b[] = data.toCharArray();

使用这个方法将字符串转换为char数组,方便遍历

num.charAt(i)-'0'

关于ascall码中字符数字转换为数字



for(i = 0;i<num.length()-1;i++) {
//如果式子里面有i+1这种操作,一定要记住不要遍历完,不然会越界

if((num.charAt(i)-'0')*10+(num.charAt(i+1)-'0')>26) {
System.out.print((char)((num.charAt(i)-'0')+64));


dfs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include<stdio.h>
int a[10],book[10],n;
//这里还有需要注意的地方C语言全局变量默认为0

void dfs(int step){ //此时在第step盒子面前,需要往里面放第i张扑克牌
int i;
if(step==n+1){ //这里说明前面的n个盒子已经放好了,这是dfs结束的标志
for(i=1;i<=n;i++)
printf("%d",a[i]);
printf("\n");

return ;
/*
注意这个 return 它的作用不是返回主函数,而是返回上一级的dfs函数

例:如果此时是 dfs(5),遇到这个 return 就会回到上一级的 dfs函数
也就是dfs(4),但此时dfs(4)的大部分语句已经执行了,只需要接着执行 book[i]=0
然后继续进入for循环进入下一次的 dfs函数,直到结束。
*/

}
for(int i=1;i<=n;i++){
if(book[i]==0){ //说明i号扑克牌还在手里,需要放入step号盒子
a[step]=i;//将i号扑克牌放到第step个盒子中
book[i]=1;//此时i号扑克牌已经被使用
dfs(step+1);
/*注意这里是自己调用自己,表示此时走到了第step+1个盒子面前*/
book[i]=0;
/*book[i]=0表示dfs调用结束了,换句话说就是扑克牌已经全部放完了
需要按照顺序将扑克牌收回,重新放,也就是前面所说的
*/
}
}
return;//这里表示这一级别的dfs函数已经结束了,返回上一级 dfs函数

}
int main(){
scanf("%d",&n);
dfs(1); //dfs函数的开始
return 0;
}

bfs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71

public class BFS {
//存放节点关系的hashtable
public static void bfs(HashMap<Character, LinkedList<Character>> graph, HashMap<Character, Integer> dist, Character s) {
//建立队列
Queue<Character> q = new LinkedList<>();
//给定起始节点
Character start = s;
//起始节点放到距离表中
dist.put(start, 0);
((LinkedList<Character>) q).add(start);
//遍历,一定要取出栈顶节点再加入
while (q != null) {
//取出栈顶节点和栈顶节点到起始节点的距离
Character poll = q.poll();
if(poll == null){
break;
}
Integer distance = dist.get(poll);
System.out.println("节点" + poll + "到起始节点" + start + "的距离为" + distance);
distance++;
//将邻接节点加入
for (Character c : graph.get(poll)) {
//如果没遍历过这个节点,加入到队列和距离表中
if (!dist.containsKey(c)) {
dist.put(c, distance);
q.offer(c);
}
}
}

}
}

核心代码
/**
* 广度优先搜索
* @param Vs 起点
* @param Vd 终点
*/
bool BFS(Node& Vs, Node& Vd){
queue<node> Q;
Node Vn, Vw;
int i;

//初始状态将起点放进队列Q
Q.push(Vs);
hash(Vw) = true;//设置节点已经访问过了!

while (!Q.empty()){//队列不为空,继续搜索!
//取出队列的头Vn
Vn = Q.front();

//从队列中移除
Q.pop();

while(Vw = Vn通过某规则能够到达的节点){
if (Vw == Vd){//找到终点了!
//把路径记录,这里没给出解法
return true;//返回
}

if (isValid(Vw) && !visit[Vw]){
//Vw是一个合法的节点并且为白色节点
Q.push(Vw);//加入队列Q
hash(Vw) = true;//设置节点颜色
}
}
}
return false;//无解
} </node>

并查集

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int find(int x)                    //查找我(x)的掌门
{
int r=x; //委托 r 去找掌门
while (pre[r ]!=r) //如果r的上级不是r自己(也就是说找到的大侠他不是掌门 = =)
r=pre[r ] ; // r 就接着找他的上级,直到找到掌门为止。
return r ; //掌门驾到~~~
}



void join(int x,int y) //我想让虚竹和周芷若做朋友
{
int fx=find(x),fy=find(y); //虚竹的老大是玄慈,芷若MM的老大是灭绝
if(fx!=fy) //玄慈和灭绝显然不是同一个人
pre[fx ]=fy; //方丈只好委委屈屈地当了师太的手下啦
}

大数

1
2
3
4
5
6
7
8
9
10
11
12
13
//大数相关知识点
//https://blog.csdn.net/qfikh/article/details/52832196

BigInteger sum = BigInteger.ZERO;
//还有 BigInteger.ONE
//还有 BigInteger.TNE
//还有 BigInteger.TWO 等大数常量
for(int i = 1;i<=n;i++) {
BigInteger iB = new BigInteger(i+"").pow(8);
//大数获取值的时候需要使用字符串,如果是个变量i 可以向上面一样变成字符串
//或者使用 BigInteger mod = BigInteger.valueOf(123456789) 这样取值
sum = sum.add(iB).mod(mod);//每次都取余一下,保证不会溢出
}

文件输出

1
2
3
4
5
6
7
//改变输出流,输出到work.txt文件
PrintStream ps = new PrintStream(new FileOutputStream("D:/work.txt"));
System.setOut(ps); //文件输出 用System.out.println()即可将内容输出到文件中
//文件流输出后直接打印就行
System.out.println("niub");
System.out.println(x + "" + ((y.compareTo(BigInteger.ZERO) > 0)? "+" : "") + y + "i");

还没整理,考完整理。。。


刷题杂谈
http://example.com/2022/04/07/perpare/
作者
Mercury
发布于
2022年4月7日
许可协议