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

42 篇文章 2 订阅
8 篇文章 0 订阅

在这里插入图片描述

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)


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;

    }

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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值