/** * 数飞机 */ public class CountPlane { public static void main(String[] args) { int n = 4; int[] arCount = new int[24]; for (int i = 0; i < arCount.length; i++) { arCount[i] = 0; } int[][] arr = { { 1, 10 }, { 2, 3 }, { 5, 8 }, { 4, 7 } }; int max = 0; for (int i = 0; i < n; i++) { for (int j = arr[i][0]; j <= arr[i][1]; j++) { arCount[j] = arCount[j] + 1; if (arCount[j] > max) { max = arCount[j]; } } } System.out.println("同时:" + max); } } /** * 判断单链表是否有环 */ public class LinkedLoop { // 内部静态类定义节点类 static class Node { int val; Node next; public Node(int val) { this.val = val; } } // 方法一,通过hashMap的存储唯一性进行判断 private static boolean hasRingOfLinked(Node head) { HashMap<Node, Node> nodeMap = new HashMap<Node, Node>(); nodeMap.put(head, head); Node temNode = head.next; while (temNode != null) { System.out.println(nodeMap.get(temNode)); if (nodeMap.get(temNode) != null) { System.out.println("--val:" + nodeMap.get(temNode).val); } if (nodeMap.get(temNode) != null) { return true; } if (temNode.next == null) { return false; } nodeMap.put(temNode, temNode); temNode = temNode.next; } return true; } // 方法二,通过环比追跑进行判断 private static boolean hasRingOfLinked2(Node head) { Node temNode1 = head; Node temNode2 = head.next; if (temNode2 == null) { return true; } while (temNode1 != temNode2) { if (temNode1.next != null) { temNode1 = temNode1.next; } else { return false; } if (temNode2.next.next != null) { temNode2 = temNode2.next.next; } else { return false; } } return true; } public static void main(String[] args) { Node head = new Node(0); Node node1 = new Node(1); Node node2 = new Node(2); Node node3 = new Node(3); Node node4 = new Node(4); Node node5 = new Node(5); Node node6 = new Node(6); head.next = node1; node1.next = node2; node2.next = node3; node3.next = node4; node4.next = node5; node5.next = node6; // node5.next = node6; // boolean res = hasRingOfLinked(head); boolean res = hasRingOfLinked2(head); System.out.println(res); } } /** * 求一个字符串中的最长不重复子串 */ public class LongestSubStr { public static void main(String[] args) { // 测试最大不重复子串函数 String str = "abcdaefgha"; // System.out.println(getLongSubStr(str)); // 测试回文字符串函数 String str2 = "abcddcba"; // System.out.println("是否回文?:" + find(str2.toCharArray(), 0, // str2.length() - 1)); // 测试分解质数 int x = 1000; System.out.print(x + " = "); prim(x, 2); } // 分解质数 private static void prim(int m, int n) { if (m >= n) { while (m % n != 0) { n++; } m /= n; prim(m, n); if (m == n) { System.out.print("*" + n); } else { System.out.print("*" + n); } } } // 递归实现判断一个字符串是否是回文字符串 private static boolean find(char[] cArr, int start, int end) { if (cArr.length == 0) { return true; } if (end <= start) { if (end == start || cArr[start] == cArr[end]) { return true; } else { return false; } } else if (cArr[start] == cArr[end]) { return find(cArr, start + 1, end - 1); } return false; } // 求一个字符串中的最大不重复子串 private static int getLongSubStr(String str) { int max = 0; int temMax = 0; // 定义一个hashMap记录每个不重复的字符 HashMap<Character, Integer> cHashMap = new HashMap<>(); // 转换为字符数组 char[] chs = str.toCharArray(); for (int i = 0; i < chs.length; i++) { // 若map集合中包含该值 if (cHashMap.containsKey(chs[i])) { if (temMax > max) { max = temMax; } // 更新临时最大值 temMax = i - cHashMap.get(chs[i]); // 更新map中最新出现该字符的位置 cHashMap.put(chs[i], i); continue; } temMax++; // 新字符 cHashMap.put(chs[i], i); } return max > temMax ? max : temMax; } } /** * 计算子数组的最大乘积 */ public class MaxMulity { public static void main(String[] args) { double[] arr = { -2.5, 3, 0, 4, 0.5, 6, -2 }; System.out.println(getMaxMulity(arr)); } private static double getMaxMulity(double[] arr) { double maxNum = arr[0]; double minNum = arr[0]; double maxEnd = arr[0]; double minEnd = arr[0]; double res = 0; for (int i = 1; i < arr.length; i++) { maxNum = maxEnd * arr[i]; minNum = minEnd * arr[i]; // 计算本次循环中的最大与最小 maxEnd = Math.max((Math.max(maxNum, minNum)), arr[i]); minEnd = Math.min((Math.min(maxNum, minNum)), arr[i]); // 再次计算最大值 res = Math.max(maxEnd, res); System.out.println("循环最大:" + res + " ,最小:" + minEnd); } return res; } } /** * 最大子数组和 */ public class MaxSumSubArrry { public static void main(String[] args) { int[] arr = { -1, -3, -2, -1, -1, -2, -2 }; int sum1 = 0; int sum2 = 0; int sumTwo = maxSum(arr, 0, 0) + maxSum(arr, 1, arr.length - 1); for (int i = 1; i < arr.length - 2; i++) { sum1 = maxSum(arr, 0, i); sum2 = maxSum(arr, i + 1, arr.length - 1); if ((sum1 + sum2) > sumTwo) { sumTwo = sum1 + sum2; } } System.out.println("两串和:" + sumTwo); } private static int maxSum(int[] arr, int start, int end) { int sum = arr[start]; int temSum = 0; int maxFlag = arr[start]; for (int i = start; i <= end; i++) { if (arr[i] > maxFlag) { maxFlag = arr[i]; } temSum += arr[i]; if (temSum < 0) { temSum = 0; continue; } if (sum < temSum) { sum = temSum; } } if (sum < maxFlag) { sum = maxFlag; } return sum; } } /** * 求一串数组的中位数 */ public class MedianOfSub { public static void main(String[] args) { int[] arr = { 3, 2, 4, 6, 5 }; int k = 3; for (int i = 0; i <= arr.length - k; i++) { System.out.println(getMedian(arr, i, k)); } } private static int getMedian(int[] arr, int start, int k) { Arrays.sort(arr, start, (start + k)); if (k % 2 == 0) { return arr[k / 2 + start - 1]; } return arr[k / 2 + start]; } } /** * 计算两个整形数组的最小的差 */ public class MinBetwwnOfTwoArr { public static void main(String[] args) { } int getMinOfTwoArr(int[] A, int[] B) { int min = Math.abs(A[0] - B[0]); Arrays.sort(A); Arrays.sort(B); for (int i = 0; i < A.length; i++) { } return min; } } /** * 求最小工人合作时间 */ public class MinWorkerTime { public static void main(String[] args) { int N = 4; int cost[][] = { { 2, 12, 5, 32 }, // 行号:任务序号,列号:工人序号 { 3, 15, 7, 11 }, // 每行元素值表示这个任务由不同工人完成所需要的时间 { 24, 18, 9, 6 }, { 21, 1, 8, 28 } }; System.out.println("最小时间:" + getMin(cost, N)); } private static int getMin(int[][] arr, int n) { int[] tAr = new int[n]; int flag = 0; for (int i = 0; i < n; i++) { int min = arr[i][0] + tAr[0]; flag = 0; for (int j = 0; j < n; j++) { if (min > (arr[i][j] + tAr[j])) { // 找出本行可参与的最小值 min = (arr[i][j] + tAr[j]); flag = j; } } tAr[flag] += arr[i][flag]; } // 循环计算出最小时间 int sum = 0; System.out.println(""); for (int i = 0; i < tAr.length; i++) { System.out.print(tAr[i] + ", "); sum += tAr[i]; } return sum; } } /** * 逆波兰表达式 */ public class RPNExpresion { public static void main(String[] args) { String exp = "(1+25)*(322+7)"; String[] strAr = getRPN(exp); System.out.print("逆波兰表达式为:"); for (int i = strAr.length - 1; i >= 0; i--) { if (strAr[i] != null) { System.out.print(strAr[i] + " "); } } } private static String[] getRPN(String expr) { String flagStr = "+-*/"; // 定义两个栈分别存储操作数和操作符 Stack<String> numStack = new Stack<String>(); Stack<String> oprStack = new Stack<String>(); // 将字符串转换为字符串数组 String[] strAr = getStrArr(expr); for (int i = 0; i < strAr.length; i++) { if (strAr[i] == null) { break; } //1、 如果是操作数,直接压入操作数栈中 if (!flagStr.contains(strAr[i]) && !strAr[i].equals("(") && !strAr[i].equals(")")) { numStack.push(strAr[i]); continue; } //2、如果是“(”,直接压入操作符栈中 if (strAr[i].equals("(")) { oprStack.push(strAr[i]); continue; } //2.1 如果是“)”,则从操作符栈中取数据,以此压入操作数栈中,直到遇到“(” if (strAr[i].equals(")")) { while (!oprStack.peek().equals("(")) { numStack.push(oprStack.pop()); } // 取走栈顶元素 oprStack.pop(); continue; } //3、 如果是操作符,则与操作符栈顶的操作符进行优先级比较 if (flagStr.contains(strAr[i])) { if (oprStack.isEmpty()) { oprStack.push(strAr[i]); continue; } if (oprStack.peek().equals("(")) { oprStack.push(strAr[i]); } else if (getPriority(strAr[i]) > getPriority(oprStack.peek())) { oprStack.push(strAr[i]); } else { numStack.push(oprStack.pop()); oprStack.push(strAr[i]); } } } // 对操作符栈进行最后判空处理 while (!oprStack.empty() && (!oprStack.peek().equals("("))) { numStack.push(oprStack.pop()); } String[] strArr2 = new String[strAr.length]; int temIndex = 0; while (!numStack.isEmpty()) { strArr2[temIndex++] = numStack.pop(); } return strArr2; } // 获取优先级函数 private static int getPriority(String chs) { if (chs.equals("-") || chs.equals("+")) { return 1; } else if (chs.equals("*") || chs.equals("/")) { return 2; }else { return 3; } } // 将字符串转换为字符串数组 private static String[] getStrArr(String str) { String[] strArr = new String[str.length()]; int index = 0; int startIndex = 0; for (int i = 0; i < str.length(); i++) { if (str.charAt(i) < '0' || str.charAt(i) > '9') { strArr[index++] = str.substring(startIndex, i + 1); startIndex = i + 1; continue; } else { if (i < str.length() - 1 && (str.charAt(i + 1) < '0' || str.charAt(i + 1) > '9')) { strArr[index++] = str.substring(startIndex, i + 1); startIndex = i + 1; continue; } } } // 对最后一个数做特殊判断处理 if (str.charAt(str.length() - 1) >= '0' && str.charAt(str.length() - 1) <= '9') { strArr[index++] = str.substring(startIndex, str.length()); } return strArr; } } /** * 筛子加和求概率 */ public class SumOfShaiZi { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); statistiSum(n); } private static void statistiSum(int n) { long[][] sum = new long[n + 1][6 * n + 1]; for (int i = 1; i <= 6; i++) { sum[1][i] = 1; } if (n >= 2) { for (int i = 2; i <= n; i++) { // 表示有多少筛子 for (int j = 1; j <= 6 * (i - 1); j++) { // 标记上个筛子的计数和最大值 for (int k = 1; k <= 6; k++) { // 对当下的筛子的1-6分别进行加数 sum[i][j + k] += sum[i - 1][(j)]; } } } } long sumCount = (long) Math.pow(6, n); DecimalFormat df = new DecimalFormat("0.00"); System.out.println("共:" + sumCount); for (int i = n; i <= n * 6; i++) { double x = ((double) sum[n][i] / sumCount + 0.0); System.out.println("[" + sum[n][i] + "," + df.format(x) + "],"); } } } /** * 任务调度,求最短时间 */ public class TaskScheduling { public static void main(String[] args) { int[] t = { 5, 2, 2, 9, 4 }; int n = 10; System.out.println(getMinTime(t, n)); } public static int getMinTime(int[] t, int n) { int len = t.length; // 1. 只有一台执行机时 if (len == 1) { return (t[0] * n); } // 2.1 初始化一份临时数组 int[] temTi = new int[len]; for (int i = 0; i < len; i++) { temTi[i] = 0; } // 2.2 循环进行计算最小加和 int temMin = 0; for (int i = 0; i < n; i++) { temMin = temTi[0] + t[0]; int index = 0; for (int k = 1; k < len; k++) { if (temMin > (temTi[k] + t[k])) { temMin = (temTi[k] + t[k]); index = k; } } temTi[index] += t[index]; } return temMin; } }
ACM
猜你喜欢
转载自blog.csdn.net/wujiandao000/article/details/79569315
今日推荐
周排行