提交 e70a63ae 编写于 作者: W wumingfang

wmf编程题

上级 b0300821
package com.example.bootdemo;
import java.util.*;
public class ShortestPathTraversal {
static int[][] points = {{1, 1}, {1, 3}, {2, 2}, {4, 4}, {2, -2},{3, -1}, {-2, 2}, {-3, 4}, {-1, -2}, {-3, -3}};
static double euclideanDistance(int[] p1, int[] p2) {
double x1 = p1[0], y1 = p1[1];
double x2 = p2[0], y2 = p2[1];
return Math.sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
}
static List<Integer> getShortestPathTraversal(int m, Set<Integer> selected) {
int n = points.length;
// 生成子集的边权数组
double[][] edges = new double[m][m];
for (int i = 0; i < m; ++i) {
int rowIdx = 0;
for (int j = 0; j < n; ++j) {
if (selected.contains(j)) {
edges[i][rowIdx++] = euclideanDistance(points[selected.toArray(new Integer[0])[i]], points[j]);
}
}
}
// 状态压缩DP
int N = (int)Math.pow(2, m);
double[][] dp = new double[N][m];
for (int i = 0; i < N; ++i) {
Arrays.fill(dp[i], Double.MAX_VALUE);
}
for (int i = 0; i < m; ++i) {
dp[1<<i][i] = 0;
}
for (int i = 1; i < N; ++i) {
for (int j = 0; j < m; ++j) {
if ((i & (1<<j)) != 0) {
int preState = i ^ (1<<j);
for (int k = 0; k < m; ++k) {
if ((preState & (1<<k)) != 0) {
dp[i][j] = Math.min(dp[i][j], dp[preState][k] + edges[k][j]);
}
}
}
}
}
// 回溯路径
List<Integer> path = new ArrayList<>();
int currState = N-1;
int currNode = 0;
for (int i = 0; i < m; ++i) {
if (dp[currState][i] < dp[currState][currNode]) {
currNode = i;
}
}
path.add(selected.toArray(new Integer[0])[currNode]);
while (currState != 1<<currNode) {
int nextState = currState ^ (1<<currNode);
int nextNode = -1;
for (int i = 0; i < m; ++i) {
if ((nextState & (1<<i)) != 0 && dp[nextState][i] + edges[currNode][i] == dp[currState][currNode]) {
nextNode = i;
break;
}
}
if (nextNode < 0) {
throw new IllegalStateException("No valid path found.");
}
path.add(selected.toArray(new Integer[0])[nextNode]);
currState = nextState;
currNode = nextNode;
}
Collections.reverse(path);
return path;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入数字的总数:"); // 先要求输入数字的总数
int n = input.nextInt();
Set<Integer> selected = new HashSet<>(n);
System.out.println("请依次输入 " + n + " 个数字:");
for (int i = 0; i < n; i++) { // 循环输入每一个数字
selected.add(input.nextInt());
}
System.out.println(getShortestPathTraversal(n, selected));
}
}
# 查找坐标点之间最短的路径
给定十个坐标点{{1, 1}, {1, 3}, {2, 2}, {4, 4}, {2, -2},{3, -1}, {-2, 2}, {-3, 4}, {-1, -2}, {-3, -3}},下标分别为0-9.
随机选择n个坐标点,n>=2,n<=10,找出遍历n个坐标点最近的路径。
## 输入描述
1、先提示输入坐标点的总个数
2、依次输入n个坐标点,注意请输入不重复的坐标点,0<=坐标点<=9
## 输出描述
输出一个数组,显示输入的n个坐标点遍历的最短路径。
## 输入样例
4
1
2
3
4
## 输出样例
[4, 2, 1, 3]
## 提示
\ No newline at end of file
[9, 8, 4, 5, 2, 3, 1, 6, 7]
\ No newline at end of file
package com.example.bootdemo;
import java.util.Scanner;
import java.util.Stack;
public class ValidParentheses {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
boolean isValid = isValid(str);
System.out.println(isValid);
}
public static boolean isValid(String s) {
if (s.length() % 2 == 1) { // 如果字符串长度为奇数则不可能完全匹配
return false;
}
Stack<Character> stack = new Stack<>();
for (char ch : s.toCharArray()) {
if (ch == '(' || ch == '[' || ch == '{') { // 左括号入栈
stack.push(ch);
} else { // 右括号出栈
if (stack.isEmpty()) { // 如果栈为空,则说明没有与该右括号匹配的左括号
return false;
}
char top = stack.peek(); // 获取栈顶元素
if ((ch == ')' && top != '(') || (ch == ']' && top != '[') || (ch == '}' && top != '{')) { // 匹配检查
return false;
}
stack.pop();
}
}
return stack.isEmpty(); // 栈内还有元素则说明不完全匹配
}
}
# 字符串判断
给定一个只包含 (、)、{、}、[、] 的字符串,判断字符串是否有效。一个字符串有效当且仅当所有括号都匹配,并且每个左括号都有相应的右括号与之对应。注意空字符串可被认为是有效字符串。
## 输入描述
输入一个字符串,字符串仅包含(、)、{、}、[、]
## 输出描述
符合题目要求输出true,不符合题目要求出书false
## 输入样例
"()"
## 输出样例
true
## 提示
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册