【Java版本】常用代码模板 ——-基础算法——- 排序 快速排序 代码模板
private void quickSort (int [] arr, int left, int right) { if (left >= right) { return arr; } int p = arr[left + right >> 1 ]; int i = left - 1 ; int j = right + 1 ; while (i < j) { while (arr[++i] < p) ; while (arr[--j] > p) ; if (i < j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } quickSort(arr, left, j); quickSort(arr, j + 1 , right); }
练习题
912. 排序数组
class Solution { public int [] sortArray(int [] nums) { quickSort(nums, 0 , nums.length - 1 ); return nums; } private int [] quickSort(int [] arr, int l, int r) { } }
归并排序 代码模板
private void mergeSort (int [] arr, int left, int right) { if (left >= right) { return arr; } int mid = left + right >> 1 ; arr = mergeSort(arr, left, mid); arr = mergeSort(arr, mid + 1 , right); int [] temp = new int [right - left + 1 ]; int i = left; int j = mid + 1 ; int k = 0 ; while (i <= mid && j <= right) { if (arr[i] <= arr[j]) { temp[k++] = arr[i++]; } else { temp[k++] = arr[j++]; } } while (i <= mid) { temp[k++] = arr[i++]; } while (j <= right) { temp[k++] = arr[j++]; } for (i = left, j = 0 ; i <= right; i++, j++) { arr[i] = temp[j]; } }
练习题
class Solution { public int [] sortArray(int [] nums) { mergeSort(nums, 0 , nums.length - 1 ); return nums; } private int [] mergeSort(int [] arr, int left, int right) { } }
二分法 整数二分 精准查找 代码模板
public int binarySearch (int [] arr, int target) { int l = -1 ; int r = nums.length; while (l + 1 != r) { int mid = l + ((r - l) >> 1 ); if (nums[mid] == target) { return mid; } else if (nums[mid] > target) { r = mid; } else { l = mid; } } return -1 ; }
练习题
class Solution { public int search (int [] nums, int target) { return binarySearch(nums, target); } public int binarySearch (int [] nums, int target) { } }
左右查找 代码模板
private int leftBinarySearch (int [] nums, int target) { int l = -1 ; int r = nums.length; while (l + 1 != r) { int mid = l + ((r - l) >> 1 ); if (nums[mid] >= target) { r = mid; } else { l = mid; } } if (r < 0 || r >= nums.length || nums[r] != target ) { r = -1 ; } return r; } private int rightBinarySearch (int [] nums, int target) { int l = -1 ; int r = nums.length; while (l + 1 != r) { int mid = l + ((r - l) >> 1 ); if (nums[mid] <= target) { l = mid; } else { r = mid; } } if (l < 0 || l >= nums.length || nums[l] != target ) { l = -1 ; } return l; }
练习题
class Solution { public int [] searchRange(int [] nums, int target) { if (null == nums || 0 == nums.length) return new int []{-1 , -1 }; int l = leftBinarySearch(nums, target); int r = rightBinarySearch(nums, target); return new int []{l, r}; } private int leftBinarySearch (int [] nums, int target) { } private int rightBinarySearch (int [] nums, int target) { } }
浮点数 private static boolean check (double x) { a } private static double EPS = 1e-6 ;private static double floatBinarySearch (double left, double right) { while (right - left > EPS) { double mid = (left + right) / 2 ; if (check(mid)) { right = mid; } else { left = mid; } } return left; }
模板题 AcWing 790. 数的三次方根
import java.util.Scanner; public class Main { public static void main (String[] args) { Scanner in = new Scanner (System.in); double x = in.nextDouble(); Double left = -Double.MAX_VALUE; Double right = Double.MAX_VALUE; while (right - left > 1e-8 ) { Double mid = (left + right) / 2 ; if (mid * mid * mid >= x) { right = mid; } else { left = mid; } } System.out.printf("%.6f" , left); } }
快速幂 递归 public int pow (int a, int n) { if (n == 0 ) { return 1 ; } else if ((n & 1 ) == 1 ) { return pow(a, n - 1 ) * a; } else { int t = pow (a, n / 2 ); return t } }
迭代 public int pow (int a, int n) { int res = 1 ; while (n > 0 ) { if ((n & 1 ) == 1 ) res * = a; a *= a; n >>= 1 ; } return res; }
高精度 高精度加法 private static List<Integer> add (List<Integer> A, List<Integer> B) { if (A.size() < B.size()) { return add(B, A); } int t = 0 ; List<Integer> C = new ArrayList <>(); for (int i = 0 ; i < A.size(); i++) { t += A.get(i); if (i < B.size()) { t += B.get(i); } C.add(t % 10 ); t /= 10 ; } if (t != 0 ) { C.add(t); } while (C.size() > 1 && C.get(C.size() - 1 ) == 0 ) { C.remove(C.size() - 1 ); } return C; }
Leetcode
暂无练习题, 可用以下代码进行练习
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; public class Main { public static void main (String[] args) throws IOException { BufferedReader in = new BufferedReader (new InputStreamReader (System.in)); String A = in.readLine(); List<Integer> aList = new ArrayList <>(); for (int i = A.length() - 1 ; i >= 0 ; i--) { aList.add(A.charAt(i) - '0' ); } String B = in.readLine(); List<Integer> bList = new ArrayList <>(); for (int i = B.length() - 1 ; i >= 0 ; i--) { bList.add(B.charAt(i) - '0' ); } List<Integer> cList = add(aList, bList); for (int i = cList.size() - 1 ; i >= 0 ; i--) { System.out.print(cList.get(i)); } in.close(); } private static List<Integer> add (List<Integer> A, List<Integer> B) { } }
高精度减法 private static boolean compare (List<Integer> A, List<Integer> B) { if (A.size() != B.size()) { return A.size() > B.size(); } for (int i = A.size() - 1 ; i >= 0 ; i--) { if (A.get(i) == B.get(i)) { continue ; } return A.get(i) > B.get(i); } return true ; } private static List<Integer> subtract (List<Integer> A, List<Integer> B) { if (!compare(A, B)) { System.out.print("-" ); return subtract(B, A); } int t = 0 ; List<Integer> C = new ArrayList <>(); for (int i = 0 ; i < A.size(); i++) { t = A.get(i) - t; if (i < B.size()) { t -= B.get(i); } C.add((t + 10 ) % 10 ); if (t < 0 ) { t = 1 ; } else { t = 0 ; } } while (C.size() > 1 && C.get(C.size() - 1 ) == 0 ) { C.remove(C.size() - 1 ); } return C; }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; public class Main { public static void main (String[] args) throws IOException { BufferedReader in = new BufferedReader (new InputStreamReader (System.in)); String s = in.readLine(); List<Integer> A = getIntegerList(s); s = in.readLine(); List<Integer> B = getIntegerList(s); List<Integer> C = subtract(A, B); for (int i = C.size() - 1 ; i >= 0 ; i--) { System.out.print(C.get(i)); } in.close(); } private static List<Integer> getIntegerList (String s) { List<Integer> res = new ArrayList <>(s.length()); for (int i = s.length() - 1 ; i >= 0 ; i--) { res.add(Character.getNumericValue(s.charAt(i))); } return res; } private static boolean compare (List<Integer> A, List<Integer> B) { if (A.size() != B.size()) { return A.size() > B.size(); } for (int i = A.size() - 1 ; i >= 0 ; i--) { if (A.get(i) == B.get(i)) { continue ; } return A.get(i) > B.get(i); } return true ; } private static List<Integer> subtract (List<Integer> A, List<Integer> B) { if (!compare(A, B)) { System.out.print("-" ); return subtract(B, A); } int t = 0 ; List<Integer> C = new ArrayList <>(); for (int i = 0 ; i < A.size(); i++) { t = A.get(i) - t; if (i < B.size()) { t -= B.get(i); } C.add((t + 10 ) % 10 ); if (t < 0 ) { t = 1 ; } else { t = 0 ; } } while (C.size() > 1 && C.get(C.size() - 1 ) == 0 ) { C.remove(C.size() - 1 ); } return C; } }
高精度乘低精度 private static List<Integer> subtract (List<Integer> A, Integer b) { List<Integer> C = new ArrayList <>(); if (b == 0 ) { C.add(0 ); return C; } int t = 0 ; for (int i = 0 ; i < A.size(); i++) { t += A.get(i) * b; C.add(t % 10 ); t /= 10 ; } if (t != 0 ) { C.add(t); } while (C.size() > 1 && C.get(C.size() - 1 ) == 0 ) { C.remove(C.size() - 1 ); } return C; }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; public class Main { public static void main (String[] args) throws IOException { BufferedReader in = new BufferedReader (new InputStreamReader (System.in)); String s = in.readLine(); List<Integer> A = getIntegerList(s); Integer b = Integer.parseInt(in.readLine()); List<Integer> C = subtract(A, b); for (int i = C.size() - 1 ; i >= 0 ; i--) { System.out.print(C.get(i)); } in.close(); } private static List<Integer> getIntegerList (String s) { List<Integer> res = new ArrayList <>(s.length()); for (int i = s.length() - 1 ; i >= 0 ; i--) { res.add(Character.getNumericValue(s.charAt(i))); } return res; } private static List<Integer> subtract (List<Integer> A, Integer b) { List<Integer> C = new ArrayList <>(); if (b == 0 ) { C.add(0 ); return C; } int t = 0 ; for (int i = 0 ; i < A.size(); i++) { t += A.get(i) * b; C.add(t % 10 ); t /= 10 ; } if (t != 0 ) { C.add(t); } while (C.size() > 1 && C.get(C.size() - 1 ) == 0 ) { C.remove(C.size() - 1 ); } return C; } }
高精度除以低精度 private static List<Integer> divide (List<Integer> A, Integer b) { int t = 0 ; List<Integer> C = new ArrayList <>(); for (int i = A.size() - 1 ; i >= 0 ; i--) { t = t * 10 + A.get(i); C.add(t / b); t %= b; } Collections.reverse(C); while (C.size() > 1 && C.get(C.size() - 1 ) == 0 ) { C.remove(C.size() - 1 ); } C.add(t); return C; }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class Main { public static void main (String[] args) throws IOException { BufferedReader in = new BufferedReader (new InputStreamReader (System.in)); String s = in.readLine(); List<Integer> A = getIntegerList(s); Integer b = Integer.parseInt(in.readLine()); List<Integer> C = divide(A, b); for (int i = C.size() - 2 ; i >= 0 ; i--) { System.out.print(C.get(i)); } System.out.println(); System.out.print(C.get(C.size() - 1 )); in.close(); } private static List<Integer> getIntegerList (String s) { List<Integer> res = new ArrayList <>(s.length()); for (int i = s.length() - 1 ; i >= 0 ; i--) { res.add(Character.getNumericValue(s.charAt(i))); } return res; } private static List<Integer> divide (List<Integer> A, Integer b) { int t = 0 ; List<Integer> C = new ArrayList <>(); for (int i = A.size() - 1 ; i >= 0 ; i--) { t = t * 10 + A.get(i); C.add(t / b); t %= b; } Collections.reverse(C); while (C.size() > 1 && C.get(C.size() - 1 ) == 0 ) { C.remove(C.size() - 1 ); } C.add(t); return C; } }
前缀和和差分 一维前缀和 代码模板
class PreSum { private int [] preSum; public PreSum (int [] nums) { preSum = new int [nums.length + 1 ]; for (int i = 1 ; i < preSum.length; i++) { preSum[i] = preSum[i - 1 ] + nums[i - 1 ]; } } public int sumRange (int left, int right) { return preSum[right + 1 ] - preSum[left]; } }
练习题
303. 区域和检索 - 数组不可变
class NumArray { PreSum preSum; public NumArray (int [] nums) { preSum = new PreSum (nums); } public int sumRange (int left, int right) { return preSum.sumRange(left, right); } } class PreSum { }
二维前缀和 代码模板
class preSum { private int [][] preSum; public preSum (int [][] matrix) { int n = matrix.length; int m = matrix[0 ].length; preSum = new int [n + 1 ][m + 1 ]; for (int i = 1 ; i <= n; i++) { for (int j = 1 ; j <= m; j++) { preSum[i][j] = preSum[i - 1 ][j] + preSum[i][j - 1 ] + matrix[i - 1 ][j -1 ] - preSum[i - 1 ][j - 1 ]; } } } public int sumRegion (int x1, int y1, int x2, int y2) { return preSum[x2 + 1 ][y2 + 1 ] - preSum[x1][y2 + 1 ] - preSum[x2 + 1 ][y1] + preSum[x1][y1]; } }
练习题
363. 矩形区域不超过 K 的最大数值和
class Solution { public int maxSumSubmatrix (int [][] matrix, int target) { int res = Integer.MIN_VALUE; preSum numMatrix = new preSum (matrix); for (int i = 0 ; i < matrix.length; i++) { for (int j = 0 ; j < matrix[0 ].length; j++) { for (int k = i; k < matrix.length; k++) { for (int l = j; l < matrix[0 ].length; l++) { int temp = numMatrix.sumRegion(i, j, k, l); if (temp > target) { continue ; }else { res = res > temp ? res : temp; } } } } } return res; } class preSum { } }
一维差分 代码模板
class Difference { int [] diff; public Difference (int [] nums) { diff = new int [nums.length]; for (int i = 1 ; i < nums.length; i++) { diff[i] = nums[i] - nums[i - 1 ]; } } public void update (int i, int j, int val) { diff[i] += val; if (j + 1 < diff.length) { diff[j + 1 ] -= val; } } public int [] result() { int [] res = new int [diff.length]; res[0 ] = diff[0 ]; for (int i = 1 ; i < res.length; i++) { res[i] = res[i - 1 ] + diff[i]; } return res; } }
练习题
1109. 航班预订统计
class Solution { public int [] corpFlightBookings(int [][] bookings, int n) { int [] res = new int [n]; Difference diff = new Difference (res); for (int i = 0 ; i < bookings.length; i++) { diff.update(bookings[i][0 ] - 1 , bookings[i][1 ] - 1 , bookings[i][2 ]); } return diff.result(); } } class Difference { }
二维差分 代码模板
class Difference { private int [][] matrix; private int [][] diff; public Difference (int [][] nums) { matrix = nums; int n = matrix.length; int m = matrix[0 ].length; diff = new int [n + 1 ][m + 1 ]; for (int i = 0 ; i < n; i++) { for (int j = 0 ; j < m; j++) { update(i, j, i, j, matrix[i][j]); } } } public void update (int x1, int y1, int x2, int y2, int val) { diff[x1][y1] += val; diff[x2 + 1 ][y1] -= val; diff[x1][y2 + 1 ] -= val; diff[x2 + 1 ][y2 + 1 ] += val; } public int [][] result() { int n = matrix.length; int m = matrix[0 ].length; int [][] res = new int [n][m]; res[0 ][0 ] = diff[0 ][0 ]; for (int i = 1 ; i < n; i++) { res[i][0 ] = res[i - 1 ][0 ] + diff[i][0 ]; } for (int i = 1 ; i < m; i++) { res[0 ][i] = res[0 ][i - 1 ] + diff[0 ][i]; } for (int i = 1 ; i < n; i++) { for (int j = 1 ; j < m; j++) { res[i][j] = res[i - 1 ][j] + res[i][j - 1 ] - res[i - 1 ][j - 1 ] + diff[i][j]; } } return res; } }
练习题
2536. 子矩阵元素加 1
class Solution { public int [][] rangeAddQueries(int n, int [][] queries) { Difference diffeMatrix = new Difference (new int [n][n]); for (int i = 0 ; i < queries.length; i++) { diffeMatrix.update(queries[i][0 ], queries[i][1 ], queries[i][2 ], queries[i][3 ], 1 ); } return diffeMatrix.result(); } class Difference { } }
位运算 位1的个数(汉明权重) 代码模板
public int hammingWeight(int n) { int count = 0; for (int i = n; i != 0; i -= (i & -i)) { count++; } return count; }
练习题
191. 位1的个数
public class Solution { public int hammingWeight (int n) { } }
双指针算法 同向指针 代码模板
int slow = 0 ;int fast = 0 ;while (fast < nums.length()) { if (check()) { slow ++; } fast ++; }
练习题
异向指针 int l = 0; int r = nums.length - 1; while (l <= r) { // do something l ++; r --; }
离散化 List<Integer> distinctSorterAlls = alls.stream().distinct().sorted() .collect(Collectors.toList()); for (int [] item : add) { int index = Collections.binarySearch(distinctSorterAlls, item[0 ]) + 1 ; a[index] += item[1 ]; } for (int i = 1 ; i <= distinctSorterAlls.size(); i++) { s[i] = s[i - 1 ] + a[i]; } for (int [] item : query) { int l = Collections.binarySearch(distinctSorterAlls, item[0 ]) + 1 ; int r = Collections.binarySearch(distinctSorterAlls, item[1 ]) + 1 ; System.out.println(s[r] - s[l - 1 ]); }
模板题 AcWing 802. 区间和
import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Scanner; import java.util.stream.Collectors; public class Main { private static final int N = 300000 ; public static void main (String[] args) { Scanner in = new Scanner (System.in); int n = in.nextInt(); int m = in.nextInt(); List<Integer> alls = new ArrayList <>(); int [] a = new int [N]; int [] s = new int [N]; List<int []> add = new ArrayList <>(); List<int []> query = new ArrayList <>(); for (int i = 0 ; i < n; i++) { int x = in.nextInt(); int c = in.nextInt(); add.add(new int []{x, c}); alls.add(x); } for (int i = 0 ; i < m; i++) { int l = in.nextInt(); int r = in.nextInt(); query.add(new int []{l, r}); alls.add(l); alls.add(r); } List<Integer> distinctSorterAlls = alls.stream().distinct().sorted() .collect(Collectors.toList()); for (int [] item : add) { int index = Collections.binarySearch(distinctSorterAlls, item[0 ]) + 1 ; a[index] += item[1 ]; } for (int i = 1 ; i <= distinctSorterAlls.size(); i++) { s[i] = s[i - 1 ] + a[i]; } for (int [] item : query) { int l = Collections.binarySearch(distinctSorterAlls, item[0 ]) + 1 ; int r = Collections.binarySearch(distinctSorterAlls, item[1 ]) + 1 ; System.out.println(s[r] - s[l - 1 ]); } } }
区间合并 代码模板
public int [][] merge(int [][] intervals) { LinkedList<int []> res = new LinkedList <>(); Arrays.sort(intervals, (a, b) -> a[0 ] - b[0 ]); res.add(intervals[0 ]); for (int i = 1 ; i < intervals.length; i++) { int [] cur = intervals[i]; int [] last = res.getLast(); if (cur[0 ] <= last[1 ]) { last[1 ] = Math.max(last[1 ], cur[1 ]); } else { res.add(cur); } } return res.toArray(new int [res.size()][2 ]); }
练习题
56. 合并区间
public int [][] merge(int [][] intervals) { }
——-图论算法——- 构建邻接表 代码模板
List<Integer>[] buildGraph(int numCourses, int [][] prerequisites) { List<Integer>[] graph = new LinkedList [numCourses]; for (int i = 0 ; i < numCourses; i++) { graph[i] = new LinkedList <>(); } for (int [] edge : prerequisites) { int from = edge[1 ], to = edge[0 ]; graph[from].add(to); } return graph; }
环路检测 代码模板
boolean [] onPath;boolean [] visited;boolean hasCycle = false ;void traverse (List<Integer>[] graph, int cur) { if (onPath[cur]) { hasCycle = true ; } if (visited[cur] || hasCycle) { return ; } visited[cur] = true ; onPath[cur] = true ; for (int next : graph[cur]) { traverse(graph, next); } onPath[cur] = false ; }
模板题
207. 课程表
class Solution { boolean [] onPath; boolean [] visited; boolean hasCycle = false ; public boolean canFinish (int numCourses, int [][] prerequisites) { List<Integer>[] graph = buildGraph(numCourses, prerequisites); visited = new boolean [numCourses]; onPath = new boolean [numCourses]; for (int i = 0 ; i < numCourses; i++) { traverse(graph, i); } return !hasCycle; } void traverse (List<Integer>[] graph, int s) { } List<Integer>[] buildGraph(int numCourses, int [][] prerequisites) { } }
拓扑排序 代码模板
boolean [] onPath;boolean [] visited;boolean hasCycle = false ;List<Integer> postorder = new ArrayList <>(); public int [] topologicalSort(int num, int [][] depends) { List<Integer>[] graph = buildGraph(num, depends); visited = new boolean [num]; onPath = new boolean [num]; for (int i = 0 ; i < num; i++) { traverse(graph, i); } if (hasCycle) { return new int []{}; } Collections.reverse(postorder); return postorder.stream().mapToInt(Integer::intValue).toArray(); } public void traverse (List<Integer>[] graph, int cur) { if (onPath[cur]) { hasCycle = true ; } if (visited[cur] || hasCycle) { return ; } visited[cur] = true ; onPath[cur] = true ; for (int next : graph[cur]) { traverse(graph, next); } postorder.add(cur); onPath[cur] = false ; }
模板题
210. 课程表 II
class Solution { public int [] findOrder(int numCourses, int [][] prerequisites) { return topologicalSort(numCourses, prerequisites); } public int [] topologicalSort(int num, int [][] depends) { } public void traverse (List<Integer>[] graph, int cur) { } List<Integer>[] buildGraph(int numCourses, int [][] prerequisites) { } }
并查集 代码模板
class UF { private int count; private int [] parent; public UF (int n) { this .count = 0 ; this .parent = new int [n]; for (int i = 0 ; i < n; i++) { parent[i] = i; } } public void union (int p, int q) { int rootP = find(p); int rootQ = find(q); if (rootP == rootQ) return ; parent[rootP] = rootQ; count--; } public boolean connected (int p, int q) { int rootP = find(p); int rootQ = find(q); return rootP == rootQ; } public int find (int x) { if (x != parent[x]) { parent[x] = find(parent[x]); } return parent[x]; } public int count () { return count; } }
练习题
990. 等式方程的可满足性
class Solution { public boolean equationsPossible (String[] equations) { UF uf = new UF (26 ); for (int i = 0 ; i < equations.length; i++) { if (getOperator(equations[i]).equals("==" )) { uf.union(getLeft(equations[i]), getRight(equations[i])); } } for (int i = 0 ; i < equations.length; i++) { if (getOperator(equations[i]).equals("!=" )) { if (uf.connected(getLeft(equations[i]), getRight(equations[i]))) return false ; } } return true ; } private String getOperator (String s) { return s.substring(1 , 3 ); } private int getLeft (String s) { return s.substring(0 , 1 ).charAt(0 ) - 'a' ; } private int getRight (String s) { return s.substring(3 ).charAt(0 ) - 'a' ; } } class UF { }
二分图 ~~~ ## -------面试常考------- ## 二叉树前中后序遍历 ### 递归 **代码模板** ~~~java public void order (List<Integer> res, TreeNode root) { if (null == root) return res.add(root.val); order(res, root.left); order(res, root.right); }
迭代 颜色标记法
白色 : 第一次访问为白色, 需要”变色”成黑色(此处为在顶部多放置一个null结点来表示变色操作)
黑色: 结点为黑色, 可直接取出进行操作
代码模板
public List<Integer> orderTraversal (TreeNode root) { if (null == root) return new LinkedList <>(); List<Integer> res = new LinkedList <>(); Deque<TreeNode> stack = new LinkedList <>(); stack.push(root); while (!stack.isEmpty()) { if (null != stack.peek()) { TreeNode node = stack.pop(); if (null != node.right) stack.push(node.right); if (null != node.left) stack.push(node.left); stack.push(node); stack.push(null ); } else { stack.pop(); TreeNode node = stack.pop(); res.add(node.val); } } return res; }
模板题
144. 二叉树的前序遍历 - 力扣(LeetCode)
94. 二叉树的中序遍历 - 力扣(LeetCode)
145. 二叉树的后序遍历 - 力扣(LeetCode)
蓝桥杯 素数 求1 - n
之间的素数
埃氏筛 public static boolean [] getPrimes(int n) { boolean [] primes = new boolean [n + 1 ]; primes[1 ] = true ; for (int i = 2 ; i <= n; i++) { if (!primes[i]) { for (int j = i * i; j <= n; j += i) { primes[j] = true ; } } } return primes; }
测试程序
public static void main (String[] args) { boolean [] result = getPrimes(100 ); IntStream.range(0 , result.length) .filter(i -> !result[i]) .forEach(i -> System.out.print(i + " " )); }
欧拉筛(线性筛) public static int [] getPrimes(int n) { int [] primes = new int [n + 1 ]; boolean [] vis = new boolean [n + 1 ]; int pos = 0 ; for (int i = 2 ; i <= n; i++) { if (!vis[i]) { primes[++pos] = i; } for (int j = 1 ; i* primes[j] <= n; j++) { vis[i * primes[j]] = true ; if (i % primes[j] == 0 ) break ; } } return primes; }
测试程序
public static void main (String[] args) { int [] result = getPrimes(100 ); Arrays.stream(result) .filter(i -> i != 0 ) .forEach(i -> System.out.print(i + " " )); }
最小公倍数和最大公约数 求最大公约数 public static long gcd (long a, long b) { long c = 0 ; while (b != 0 ) { c = a % b; a = b; b = c; } return a; }
测试程序
public static void main (String[] args) { long a = 15 ; long b = 12 ; System.out.println(gcd(a, b)); }
求最小公倍数 同上求最大公约数
测试程序
public static void main (String[] args) { long a = 15 ; long b = 12 ; System.out.println(a * b / gcd(a, b)); }
快速输入输出模板 import java.io.BufferedInputStream;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.io.PrintWriter;import java.io.StreamTokenizer;public class Template { public static BufferedWriter out = new BufferedWriter (new OutputStreamWriter (System.out)); public static BufferedReader in = new BufferedReader (new InputStreamReader (System.in)); public static StreamTokenizer cin = new StreamTokenizer (new BufferedReader (new InputStreamReader (System.in))); public static PrintWriter cout = new PrintWriter (new OutputStreamWriter (System.out)); static { cin.ordinaryChars('0' , '0' ); cin.wordChars('0' , '9' ); cin.ordinaryChar('-' ); cin.wordChars('-' , '-' ); } public static void main (String[] args) throws Exception{ cout.flush(); closeAll(); } public static int nextInt () throws Exception { cin.nextToken(); return Integer.parseInt(cin.sval); } public static long nextLong () throws Exception { cin.nextToken(); return Long.parseLong(cin.sval); } public static double nextDouble () throws Exception { cin.nextToken(); return Double.parseDouble(cin.sval); } public static String nextString () throws Exception { cin.nextToken(); return cin.sval; } public static void closeAll () throws Exception { in.close(); out.close(); cout.close(); } }