1)
package A;
public class LetterCombineTest {
/*
回溯
*/
public static void dfs(char[] letterList, int start) {
boolean[] choices = {true, false};
if (start == letterList.length) {
System.out.println(letterList);
return;
}
char c = letterList[start];
if (isEnglishLetter(c)) {
for (int i = 0; i < 2; i++) {
letterList[start] = choices[i] ? Character.toLowerCase(c) : Character.toUpperCase(c);
dfs(letterList, start + 1);
}
} else {
dfs(letterList, start + 1);
}
}
/**
* 是否是英文字母
*
* @param c
* @return
*/
public static boolean isEnglishLetter(char c) {
if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z') {
return true;
}
return false;
}
public static void main(String[] args) {
String str = "012e4s";
// 查找英文字母
char[] letterList = str.toCharArray();
//
dfs(letterList, 0);
}
}
/*
012e4s
012e4S
012E4s
012E4S
*/
2)
Sort.java
package B;
public class Sort {
public static void mySort(char[] arr, int left, int right) {
int l = left;
int r = right;
int pivot = arr[(left + right) / 2];
char temp = 0;
while (l < r) {
while (arr[l] < pivot) {
l++;
}
while (arr[r] > pivot) {
r--;
}
if (l >= r) {
break;
}
temp = arr[l];
arr[l] = arr[r];
arr[r] = temp;
if (arr[l] == pivot) {
r--;
}
if (arr[r] == pivot) {
l++;
}
}
if (l == r) {
l += 1;
r -= 1;
}
if (left < r) {
mySort(arr, left, r);
}
if (right > l) {
mySort(arr, l, right);
}
}
public static void sort(char[] chars) {
mySort(chars, 0, chars.length - 1);
}
}
SortTest.java
package B;
public class SortTest {
public static void main(String[] args) {
char[] charsList = {'a', 'p', 'p', 'l', 'e'};
Sort.sort(charsList);
System.out.println(charsList);
}
}
/*
aelpp
*/
3)
RoleData.java
package C;
import java.util.Arrays;
public class RoleData {
private float[] properties;
public RoleData(float[] properties) {
this.properties = properties;
if (properties.length != 5) {
throw new RuntimeException("角色需要有5个属性");
}
}
/**
* 根据策划配置的表格,赋予每一种能力加一个权重, 从而估算出实力
*
* @return
*/
public float getAbalityValue() {
return (float) (properties[0] * 1 + properties[1] * 1.1 + properties[2] * 1.2 + properties[3] * 1.3 + properties[4] * 1.4);
}
@Override
public String toString() {
return "RoleData{" +
"properties=" + Arrays.toString(properties) +
'}';
}
}
RoleUtil.java
package C;
import java.util.List;
public class RoleUtil {
public static RoleData find(List<RoleData> roleDataList, int inputRoleDataListIndex) {
// 默认第一个是最小差距的
RoleData minDiffRoleData = null;
float minDiff = 0;
float targetAbality = roleDataList.get(inputRoleDataListIndex).getAbalityValue();
for (int i = 0; i < roleDataList.size(); i++) {
// 不能和自己比较
if (i == inputRoleDataListIndex) {
continue;
}
if (minDiffRoleData == null) {
minDiffRoleData = roleDataList.get(i);
minDiff = Math.abs(targetAbality - minDiffRoleData.getAbalityValue());
} else {
RoleData curRoleData = roleDataList.get(i);
float curDiff = Math.abs(targetAbality - curRoleData.getAbalityValue());
if (curDiff < minDiff) {
minDiffRoleData = curRoleData;
minDiff = curDiff;
}
}
}
return minDiffRoleData;
}
}
RoleTest.java
package C;
import java.util.ArrayList;
import java.util.Arrays;
public class RoleTest {
public static void main(String[] args) {
ArrayList<RoleData> roleDataList = new ArrayList<>(Arrays.asList(
new RoleData(new float[]{1, 1, 1, 1, 1}),
new RoleData(new float[]{2, 3, 4, 5, 6}),
new RoleData(new float[]{10, 11, 11, 10, 12}),
new RoleData(new float[]{16, 17, 18, 19, 19}),
new RoleData(new float[]{7, 8, 9, 10, 11}),
new RoleData(new float[]{10, 11, 11, 11, 12})
));
RoleData targetRoleData = RoleUtil.find(roleDataList, roleDataList.size() - 1);
System.out.println("输入的角色信息:\n" + roleDataList.get(roleDataList.size() - 1));
System.out.println("\n最接近的角色信息:\n" + targetRoleData);
}
}
/*
输入的角色信息:
RoleData{properties=[10.0, 11.0, 11.0, 11.0, 12.0]}
最接近的角色信息:
RoleData{properties=[10.0, 11.0, 11.0, 10.0, 12.0]}
*/
4)
CoronaVirus.java
package D;
import java.util.HashSet;
import java.util.Set;
/**
* 思路:
* 1.如果是0,且周围有3,那么就标记被感染(标记为3)
* 2.如果是1,那么就跳过
* 3.如果是2,且周围有3,那么游戏结束,Rump被感染了
* 4.如果是3,那么跳过
*/
public class CoronaVirus {
/**
* 0表示没戴口罩议员
* 1表示已戴口罩议员
* 2表示没戴口罩Rump总统
* 3表示没戴口罩且染毒议员
*
* @param map
* @param width
* @param height
* @return
*/
public static int detect(int[] map, int width, int height) {
int targetIndex = getRumpIndex(map);
int time = 0;
while (true) {
Set<Integer> effectSet = getNextEffectIndexSet(map, width, height);
// 如果川普被感染了,那么就直接返回,游戏结束
if (effectSet.contains(targetIndex)) {
time++;
return time;
} else {
// 如果没有人被感染了,那么川普不会被感染,游戏结束
if (effectSet.size() == 0) {
return 0;
}
// 有人感染,进入下一轮感染
time++;
for (int index : effectSet) {
map[index] = 3;
}
}
}
}
/**
* 得到川普位置
*
* @param map
* @return
*/
private static int getRumpIndex(int[] map) {
for (int i = 0; i < map.length; i++) {
if (map[i] == 2) {
return i;
}
}
throw new RuntimeException("没有找到川普,地图数据有误");
}
/**
* 索引n周围是否有病毒
*
* @param map
* @param n
* @return
*/
private static boolean isAroundHaveVirus(int[] map, int n, int width, int height) {
// 只有没带口罩的或者川普才会被感染
if (map[n] != 0 && map[n] != 2) {
return false;
}
// 周围的索引
int[] aroundIndexList = {
n - width - 1, n - width, n - width + 1,
n - 1, n + 1,
n + width - 1, n + width, n + width + 1
};
for (int index : aroundIndexList) {
if (index >= 0 && index < map.length) {
int nRow = n / width;
int nCol = n % width;
int indexRow = index / width;
int indexCol = index % width;
if (Math.abs(nRow - indexRow) <= 1 && Math.abs(nCol - indexCol) <= 1) {
if (map[index] == 3) {
return true;
}
}
}
}
return false;
}
/**
* 被感染的索引
*
* @param map
* @return
*/
private static Set<Integer> getNextEffectIndexSet(int[] map, int width, int height) {
Set<Integer> indexSet = new HashSet<>();
for (int n = 0; n < map.length; n++) {
if (isAroundHaveVirus(map, n, width, height)) {
indexSet.add(n);
}
}
return indexSet;
}
}
CoronaVirusTest.java
package D;
public class CoronaVirusTest {
public static void main(String[] args) {
int[] map = new int[]{
0, 0, 0, 2, 0, 0, 0, 0,
0, 1, 1, 0, 0, 0, 0, 0,
0, 0, 1, 1, 1, 0, 0, 1,
0, 1, 1, 0, 0, 0, 0, 0,
0, 1, 0, 0, 1, 1, 1, 0,
0, 1, 0, 0, 1, 0, 0, 0,
0, 0, 0, 0, 1, 0, 3, 0,
1, 0, 1, 0, 0, 0, 0, 0,
};
int time = CoronaVirus.detect(map, 8, 8);
System.out.println(time);
}
}
/*
6
*/