/**
* 數飛機
*/
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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.