From c65db22f67af8018ae7105ea00d8743e42835e13 Mon Sep 17 00:00:00 2001 From: ZY <zc857179121@qq.com> Date: 星期六, 29 三月 2025 15:52:09 +0800 Subject: [PATCH] 1 --- src/main/java/com/zy/common/model/Shelves.java | 54 +++++++++++++++++++++++++++++++++++++++++------------- 1 files changed, 41 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/zy/common/model/Shelves.java b/src/main/java/com/zy/common/model/Shelves.java index f8cebc4..5068e65 100644 --- a/src/main/java/com/zy/common/model/Shelves.java +++ b/src/main/java/com/zy/common/model/Shelves.java @@ -1,30 +1,39 @@ package com.zy.common.model; +import com.alibaba.fastjson.JSON; +import com.core.exception.CoolException; + import java.util.ArrayList; import java.util.Iterator; import java.util.List; /** + * <strong>绔嬪簱璐ф灦瀹炰綋绫�</strong> * Created by vincent on 2020/6/11 */ public class Shelves { // 璐ф灦鎺掓暟閲� - private final int size; + public final int size; // 璐ф灦缁勬暟閲� - private final int group; + public final int group; // 鍋忕Щ閲廩default:0] - private int offset = 0; + public final int offset; - // 璐ф灦瀹炰緥鑺傜偣 - private List<List<Integer>> nodes; + // 璐ф灦瀹炰緥鑺傜偣闆嗗悎 + public List<List<Integer>> nodes; public Shelves(int size, int group) { this(size, group, 0); } + /** + * @param size 璐ф灦鍗曟帓鎬绘暟 + * @param group 璐ф灦缁勬暟閲� + * @param offset 搴忓垪鍙峰亸绉婚噺 + */ public Shelves(int size, int group, int offset) { this.size = size; this.group = group; @@ -33,15 +42,15 @@ } /** - * 鍒濆鍖� + * 鍒濆鍖栨柟娉曘�愮鏈夈�� */ private void init(){ if (group == 0 || size%group != 0) { throw new RuntimeException("shelves init fail!"); } nodes = new ArrayList<>(); - for (int g = 1; g <= this.group; g++){ // 1 2 - int unit = size/group; // 4 + for (int g = 1; g <= this.group; g++){ + int unit = size/group; List<Integer> node = new ArrayList<>(); for (int i = (g-1)*unit+1+offset ; i <= g*unit+offset; i++){ node.add(i); @@ -51,6 +60,14 @@ } /** + * 寮�濮嬭绠� =======>>> + * + * 璐ф灦鍛戒腑瑙勫垯濡備笅锛� + * 瀹夎浣嶇疆锛� [1] [2] | [3] [4] -------- [5] [6] | [7] [8] + * 鍛戒腑椤哄簭锛� 1 -> 5 -> 4 -> 8 -> 2 -> 6 -> 3 -> 7 -> 1 ... + * + * 1.璇ヨ鍒欓�備笉闄愬埗璐ф灦鏁伴噺锛屾�绘暟涓庣粍鍒湪鏋勯�犲櫒涓缃� + * 2.濡傛湁搴忓垪鍙疯捣濮嬮棶棰橈紝鐢ㄥ亸绉婚噺瑙勯伩鍗冲彲 * * @param curSeq 褰撳墠璐ф灦鍙� * @return 瑙勫垯鍛戒腑璐ф灦鍙� @@ -66,8 +83,8 @@ return iterator.next().get(idx); } else { List<Integer> first = nodes.get(0); - Integer val = first.get(idx); - int res = size /group + 1 + offset - val; + int val = first.get(idx); + int res = size / group + 1 + offset - val; // 鍙嶅悜鍛戒腑璐ф灦鏃朵笉鍐嶆槸瀵圭珛涓嬫爣锛堢浉瀵逛簬宸烽亾锛� if (res < val) { // 杞鎵�鏈夎揣鏋跺悗閲嶆柊寮�濮嬪畾浣� @@ -80,16 +97,27 @@ } } } - return 0; + return -1; + } + + public Integer get(Integer curRow) { + for (List<Integer> node : nodes){ + if (node.contains(curRow)) { + return nodes.indexOf(node) + 1; + } + } + throw new CoolException("璐ф帓妫�绱㈢郴缁熸姤閿欙紝 node:" + JSON.toJSONString(nodes) + ", curRow:" + curRow); } public static void main(String[] args) throws InterruptedException { - Shelves shelves = new Shelves(4,2); + Shelves shelves = new Shelves(2,1); System.out.println(shelves.nodes.toString()); int start = 1; while (true) { - System.out.println(start); +// System.out.println(start); start = shelves.start(start); + Integer crnNo1 = shelves.get(start); + System.out.println(crnNo1); Thread.sleep(500L); } } -- Gitblit v1.9.1