紫龍算法題目

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
 */

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章