一些常用的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) { return o2-o1; } }); Arrays.sort(a, l2, r2 + 1 ,(o1, o2) -> {return o2 - o1;}) Collections.sort(list, new Comparator<Dog>() { @Override public int compare (Dog o1, Dog o2) { return o1.name.compareTo(o2.name); } }); System.out.println("给狗狗按名字字母顺序排序:" +list); }
最大公约数
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.to CharArray() ; 使用这个方法将字符串转换为char 数组,方便遍历 num.char At(i ) -'0' 关于ascall码中字符数字转换为数字for (i = 0 ;i<num.length() -1 ;i++) { if ((num.char At(i ) -'0' )*10 +(num.char At(i +1) -'0' )>26 ) { System . out.print((char )((num.char At(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;void dfs (int step) { int i; if (step==n+1 ){ for (i=1 ;i<=n;i++) printf ("%d" ,a[i]); printf ("\n" ); return ; } for (int i=1 ;i<=n;i++){ if (book[i]==0 ){ a[step]=i; book[i]=1 ; dfs (step+1 ); book[i]=0 ; } } return ; }int main () { scanf ("%d" ,&n); dfs (1 ); 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 { 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); } } } } } 核心代码 bool BFS(Node& Vs, Node& Vd){ queue<node> Q; Node Vn, Vw; int i; Q.push(Vs); hash(Vw) = true ; while (!Q.empty()){ Vn = Q.front(); Q.pop(); while (Vw = Vn通过某规则能够到达的节点){ if (Vw == Vd){ return true ; } if (isValid(Vw) && !visit[Vw]){ Q.push(Vw); 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) { int r=x; while (pre[r ]!=r) r=pre[r ] ; return r ; }void join (int x,int y) { int fx=find (x),fy=find (y); 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 PrintStream ps = new PrintStream(new FileOutputStream("D:/work.txt" ) ); System . setOut(ps ) ; System . out.println("niub" ); System . out.println(x + "" + ((y.compareTo(BigInteger.ZERO) > 0 )? "+" : "" ) + y + "i" );
还没整理,考完整理。。。