• 骑马钉 根据列行页数 生成 排序规则 java版 JavaScript版 python版


    在这里插入图片描述

    python

    RULE = {'2x1 4': '4 1 2 3',
            '2x2 8': '1* 8* 4 5 7* 2* 6 3',
            '2x2 4': '1* 4* 2 3',
            '2x3 12': '1* 12* 4 9 5* 8* 11* 2* 10 3 7* 6*',
            '4x1 8': '4 5 8 1 2 7 6 3',
            '4x1 4': '2 3 4 1',
    
            '4x2 16': '1* 16* 13* 4* 8 9 12 5 3* 14* 15* 2* 6 11 10 7',
            '4x2 8': '1* 8* 7* 2* 4 5 6 3',
            '4x2 4': '2 3 4 1 2 3 4 1',
    
            '2x4 16': '1* 16* 4 13 5* 12* 8 9 15* 2* 14 3 11* 6* 10 7',
            '2x4 8': '1* 8* 4 5 3* 6* 2 7',
            '2x4 4': '1* 4* 1* 4* 2 3 2 3',
    
            '4x3 12': '7* 6* 5* 8* 10 3 4 9 11* 2* 1* 12*',
            '4x4 32': '13* 20* 21* 12* 4 29 28 5 1* 32* 25* 8* 16 17 24 9 11* 22* 19* 14* 6 27 30 3 7* 26* 31* 2* 10 23 18 15',
            '4x4 16': '5* 12* 9* 8* 4 13 16 1 3* 14* 15* 2* 6 11 10 7',
            '4x4 8': '1* 8* 5* 4* 1* 8* 5* 4* 2 7 6 3 2 7 6 3',
            '4x4 4': '1* 4* 1* 4* 1* 4* 1* 4* 2 3 2 3 2 3 2 3'
            }
    
    
    def saddleStitch(col, row, page_count):
        temp_page_count = page_count
        _list = []
        # 拼数
        spell = col * row * 2
        # 余数
        remainder = page_count % spell
        if spell == 12 and remainder != 0: return
        if spell == 24 and spell % 12 != 0: return
        if remainder != 0 and spell != 24:
            if (spell == 4 or spell == 8 or spell == 16 or spell == 32):
                if remainder == 4 or remainder == 8:
                    _list.append(remainder)
                elif remainder == 12:
                    _list.append(4)
                    _list.append(8)
                elif remainder == 16:
                    _list.append(16)
                elif remainder == 20:
                    _list.append(4)
                    _list.append(16)
                elif remainder == 24:
                    _list.append(8)
                    _list.append(16)
                elif remainder == 28:
                    _list.append(4)
                    _list.append(8)
                    _list.append(16)
                else:
                    return
            else:
                return
    
            page_count -= remainder
    
        if spell == 24:
            for i in range(int(page_count / (col * row))):
                _list.append(col * row)
        else:
            for i in range(int(page_count / (col * row * 2))):
                _list.append(col * row * 2)
    
        list_result = []
        start = 0
        for index, item in enumerate(_list):
            len = item / 2
            before_start = int(start + 1)
            before_end = int(start + len)
            before = '%d-%d' % (before_start, before_end)
    
            after_start = int(temp_page_count - start - len + 1)
            after_end = int(temp_page_count - start)
            after = '%d-%d' % (after_start, after_end)
            start += len
            key = '%dx%d %d' % (col, row, item)
            print_model = '正反' if item == spell else '自翻'
            key_rule = '%d-%s-%dx%d %d %s %s' % (index, print_model, col, row, item, before, after)
            s = RULE[key]
            if s is None: return
            value = ''
            for item in s.split(' '):
                page = int(item.replace('*', ''))
                xx = "*" if "*" in item else ""
                if page > len:
                    value += '%d%s>' % (int(after_end - len * 2 + page), xx)
                else:
                    value += '%d%s>' % (int(before_start - 1 + page), xx)
    
            list_result.append('%s %s' % (key_rule, value))
    
        return list_result
    
    
    def getRule(col, row, page_count):
        import re
        list_result = saddleStitch(col, row, page_count)
        if list_result is not None and len(list_result) > 0:
            value = ''
            for item in list_result:
                arr = re.split(' |x|-', item)
                value += arr[len(arr) - 1].replace('>', " ")
            return value
    
    
    col = 2
    row = 2
    page_count = 44
    print('输入页数:%d,列行:%dx%d' % (page_count,col,row))
    list_result = saddleStitch(col, row, page_count)
    print(list_result)
    rule = getRule(col, row, page_count)
    print(rule)
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117

    Java

    public static void main(String[] args) {
            List<String> dd = qimading(4, 4, 44);
            System.out.println(dd);
            String rule = getRule(4, 4, 44);
            System.out.println(rule);
    
    
    
        }
        public static Map<String,String> RULE = new HashMap<>();
        static {
            RULE.put("2x1 4","4 1 2 3");
    
            RULE.put("2x2 8","1* 8* 4 5 7* 2* 6 3");
            RULE.put("2x2 4","1* 4* 2 3");
            RULE.put("2x3 12","1* 12* 4 9 5* 8* 11* 2* 10 3 7* 6*");
            RULE.put("4x1 8","4 5 8 1 2 7 6 3");
            RULE.put("4x1 4","2 3 4 1");
    
            RULE.put("4x2 16","1* 16* 13* 4* 8 9 12 5 3* 14* 15* 2* 6 11 10 7");
            RULE.put("4x2 8","1* 8* 7* 2* 4 5 6 3");
            RULE.put("4x2 4","2 3 4 1 2 3 4 1");
    
            RULE.put("2x4 16","1* 16* 4 13 5* 12* 8 9 15* 2* 14 3 11* 6* 10 7");
            RULE.put("2x4 8","1* 8* 4 5 3* 6* 2 7");
            RULE.put("2x4 4","1* 4* 1* 4* 2 3 2 3");
    
            RULE.put("4x3 12","7* 6* 5* 8* 10 3 4 9 11* 2* 1* 12*");
    
    
            RULE.put("4x4 32","13* 20* 21* 12* 4 29 28 5 1* 32* 25* 8* 16 17 24 9 11* 22* 19* 14* 6 27 30 3 7* 26* 31* 2* 10 23 18 15");
            RULE.put("4x4 16","5* 12* 9* 8* 4 13 16 1 3* 14* 15* 2* 6 11 10 7");
            RULE.put("4x4 8","1* 8* 5* 4* 1* 8* 5* 4* 2 7 6 3 2 7 6 3");
            RULE.put("4x4 4","1* 4* 1* 4* 1* 4* 1* 4* 2 3 2 3 2 3 2 3");
        }
        public static String getRule(int col,int row,int page_count){
            List<String> qimading = qimading(col, row, page_count);
            if(qimading!=null&&qimading.size()>0){
                String value ="";
                for(String str:qimading){
                    String[] arr = str.split(" |x|-");
                    String r = arr[arr.length-1];
                    r= r.replaceAll(">"," ");
                    value += r;
                }
                return value;
            }
            return "获取规则出错";
    
        }
        public static List<String> qimading(int col, int row, int page_count){
            int temp_page_count = page_count;
            List<Integer> list = new ArrayList<>();
            int spell = col*row*2;
            int yushu = page_count % spell;
            if(spell==12 && yushu!=0)return null;
            if(spell==24&&spell%12!=0)return null;
            if (yushu!=0&&spell!=24){
                if(spell==4||spell==8||spell==16||spell==32){
                    if(yushu ==4 || yushu == 8){
                        list.add(yushu);
                    }else  if(yushu == 12){
                        list.add(4);
                        list.add(8);
                    }else if (yushu == 16){
                        list.add(16);
                    }else if (yushu == 20){
                        list.add(4);
                        list.add(16);
                    }else if (yushu == 24){
                        list.add(8);
                        list.add(16);
                    }else if (yushu == 28){
                        list.add(4);
                        list.add(8);
                        list.add(16);
                    }else{
                        return null;
                    }
                }else {
                    return null;
                }
    
                page_count -= yushu;
            }
    
    
            if(spell==24){
                for (int i = 0; i < page_count/(col * row); i++) {
                    list.add(col * row);
                }
            }else{
                for (int i = 0; i < page_count/(col * row * 2); i++) {
                    list.add(col * row * 2);
                }
            }
    
            List<String> listResult = new ArrayList<>();
            int start = 0;
            for (int i = 0; i < list.size(); i++) {
                int len = list.get(i)/2;
                int beforeStart = start+1;
                int beforeEnd = start+len;
                String before =beforeStart+"-"+beforeEnd;
                int afterStart = temp_page_count-start-len+1;
                int afterEnd = temp_page_count-start;
                String after = afterStart+"-"+afterEnd;
                start +=len;
                String key = col+"x"+row+" "+list.get(i);
                String  print_model = list.get(i)==spell?"正反":"自翻";
                String key_rule = i+"-"+print_model+"-"+col+"x"+row+" "+list.get(i)+" "+before+" "+after;
                String s = RULE.get(key);
    
                if(s== null)return null;
                String value ="";
                for(String str:s.split(" ")){
                    int page = Integer.valueOf(str.replaceAll("\\*",""));
                    String xx = str.indexOf("*")==-1?"":"*";
                    if(page>len){
                        value+= (afterEnd-len*2+page)+xx+">";
                    }else{
                        value+= (beforeStart-1+page)+xx+">";
                    }
                }
                listResult.add(key_rule+" "+value);
            }
            return listResult;
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129

    JavaScript

    RULE = {
        '2x1 4': '4 1 2 3',
        '2x2 8': '1* 8* 4 5 7* 2* 6 3',
        '2x2 4': '1* 4* 2 3',
        '2x3 12': '1* 12* 4 9 5* 8* 11* 2* 10 3 7* 6*',
        '4x1 8': '4 5 8 1 2 7 6 3',
        '4x1 4': '2 3 4 1',
    
        '4x2 16': '1* 16* 13* 4* 8 9 12 5 3* 14* 15* 2* 6 11 10 7',
        '4x2 8': '1* 8* 7* 2* 4 5 6 3',
        '4x2 4': '2 3 4 1 2 3 4 1',
    
        '2x4 16': '1* 16* 4 13 5* 12* 8 9 15* 2* 14 3 11* 6* 10 7',
        '2x4 8': '1* 8* 4 5 3* 6* 2 7',
        '2x4 4': '1* 4* 1* 4* 2 3 2 3',
    
        '4x3 12': '7* 6* 5* 8* 10 3 4 9 11* 2* 1* 12*',
        '4x4 32': '13* 20* 21* 12* 4 29 28 5 1* 32* 25* 8* 16 17 24 9 11* 22* 19* 14* 6 27 30 3 7* 26* 31* 2* 10 23 18 15',
        '4x4 16': '5* 12* 9* 8* 4 13 16 1 3* 14* 15* 2* 6 11 10 7',
        '4x4 8': '1* 8* 5* 4* 1* 8* 5* 4* 2 7 6 3 2 7 6 3',
        '4x4 4': '1* 4* 1* 4* 1* 4* 1* 4* 2 3 2 3 2 3 2 3'
    }
    
    function saddleStitch(col, row, page_count) {
        temp_page_count = page_count
        _list = []
        //拼数
        spell = col * row * 2
        //余数
        remainder = page_count % spell
        if (spell == 12 && remainder != 0) return
        if (spell == 24 && spell % 12 != 0) return
        if (remainder != 0 && spell != 24) {
            if (spell == 4 || spell == 8 || spell == 16 || spell == 32) {
                if (remainder == 4 || remainder == 8) {
                    _list.push(remainder)
                } else if (remainder == 12) {
                    _list.push(4)
                    _list.push(8)
                } else if (remainder == 16) {
                    _list.push(16)
                } else if (remainder == 20) {
                    _list.push(4)
                    _list.push(16)
                } else if (remainder == 24) {
                    _list.push(8)
                    _list.push(16)
                } else if (remainder == 28) {
                    _list.push(4)
                    _list.push(8)
                    _list.push(16)
                } else {
                    return
                }
            } else {
                return
            }
            page_count -= remainder
        }
        if (spell == 24) {
            for (var i = 0; i < parseInt(page_count / (col * row)); i++) _list.push(row * col)
        } else {
            for (var i = 0; i < parseInt(page_count / (col * row * 2)); i++) _list.push(row * col * 2)
        }
        list_result = []
        start = 0
        for (var i = 0; i < _list.length; i++) {
            len = _list[i] / 2
            before_start = parseInt(start + 1)
            before_end = parseInt(start + len)
            before = before_start + '-' + before_end
    
            after_start = parseInt(temp_page_count - start - len + 1)
            after_end = parseInt(temp_page_count - start)
            after = after_start + "-" + after_end
            start += len
            key = col + "x" + row + " " + _list[i]
            print_model = _list[i] == spell ? '正反' : '自翻'
            key_rule = i + "-" + print_model + "-" + col + "x" + row + " " + _list[i] + ' ' + before + " " + after
            s = RULE[key]
    
            if (s == undefined) return
    
            value = ''
            for (var j = 0; j < s.split(' ').length; j++) {
                item = s.split(' ')[j]
                page = parseInt(item.replace("*", ''))
                xx = item.indexOf("*") == -1 ? '' : '*'
    
                if (page > len) {
                    value += parseInt(after_end - len * 2 + page) + xx + '>'
                } else {
                    value += parseInt(before_start - 1 + page) + xx + '>'
                }
            }
            list_result.push(key_rule + ' ' + value)
        }
        return list_result
    
    }
    
    function getRule(col, row, page_count) {
        list_result = saddleStitch(col, row, page_count);
        if (list_result != undefined && list_result.length > 0) {
            value = ''
            for (var i = 0; i < list_result.length; i++) {
                item = list_result[i];
                arr = item.split(/ |x|-/);
                value += arr[arr.length - 1].replace(/>/g, " ")
            }
            return value
        }
    }
    
    col = 2
    row = 2
    page_count = 44
    list_result = saddleStitch(col, row, page_count)
    alert(list_result)
    rule = getRule(col, row, page_count)
    alert(rule)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
  • 相关阅读:
    Linux的简单使用
    二十三种设计模式全面解析-工厂模式:创造对象的魔法工厂
    LeetCode_栈_中等_227.基本计算器 II(不含括号)
    Codeforces Round #796 (Div. 2) A. Cirno‘s Perfect Bitmasks Classroom
    Qt Designer基础控件介绍
    Ansible的脚本 --- playbook 剧本
    Vue响应式内容丢失处理
    并发工具类——CountDownLatch、CyclicBarrier、Semaphore、Exchanger的介绍与使用
    HTML+CSS+JS简易计算器
    秒杀项目的总结及面试常见问题
  • 原文地址:https://blog.csdn.net/jialan75/article/details/127583607