九宫格00

import math
 
# 求解九宫格
def sudoku():
    nums_list = input().split(" ")
    nums_list = [int(num) for num in nums_list]
 
    mul_item, d_list = search_tuple(nums_list)
    ans_list = arrange_tuple(mul_item, d_list)
    ans_list = sort_ans_list(ans_list)
    print_ans_list(ans_list)
 
 
# 求解幻积并统计符合规律的每个数字出现的次数
def search_tuple(nums_list):
    # 求解幻积
    total_mul = 1
    for num in nums_list:
        total_mul *= num
 
    # 幻积
    mul_item = round(math.pow(total_mul, (1 / 3)))
    nums_list.sort()
 
    # 寻找符合幻积的数字对,并统计每个数字出现的次数
    d = {}
    length = len(nums_list)
    for i in range(length - 2):
        for j in range(i + 1, length - 1):
            for k in range(j + 1, length):
                if nums_list[i] * nums_list[j] * nums_list[k] == mul_item:
                    d[nums_list[i]] = d[nums_list[i]] + 1 if nums_list[i] in d.keys() else 1
                    d[nums_list[j]] = d[nums_list[j]] + 1 if nums_list[j] in d.keys() else 1
                    d[nums_list[k]] = d[nums_list[k]] + 1 if nums_list[k] in d.keys() else 1
 
    d_list = sorted(d.items(), key=lambda x: x[1], reverse=True)
    return mul_item, d_list
 
 
# 根据各个数字出现的次数排列9个数字的顺序
def arrange_tuple(mul_item, d_list):
    ans_list = []
    for i in range(5):
        if i == 2:
            continue
        tmp = [0 for _ in range(9)]
        tmp[4] = d_list[0][0]
        tmp[2 * i] = d_list[1][0]
 
        other_value = 0
        for item in d_list[2: 5]:
            if tmp[2 * i] * tmp[4] * item[0] == mul_item:
                other_value = item[0]
        tmp[8 - 2 * i] = other_value
 
        line_list = []
        for item in d_list[2: 5]:
            if item[0] != tmp[2 * i] and item[0] != tmp[8 - 2 * i]:
                line_list.append(item[0])
 
        if 2 * i == 0 or 2 * i == 8:
            location_list = [2, 6]
        else:
            location_list = [0, 8]
        for j in range(2):
            tmp = tmp.copy()
            if j == 0:
                tmp[location_list[0]] = line_list[0]
                tmp[location_list[1]] = line_list[1]
            else:
                tmp[location_list[0]] = line_list[1]
                tmp[location_list[1]] = line_list[0]
 
            tmp[1] = mul_item // (tmp[0] * tmp[2])
            tmp[3] = mul_item // (tmp[0] * tmp[6])
            tmp[5] = mul_item // (tmp[2] * tmp[8])
            tmp[7] = mul_item // (tmp[6] * tmp[8])
 
            ans_list.append(tmp)
 
    return ans_list
 
 
# 排序
def sort_ans_list(ans_list):
    ans_list.sort(key=lambda x: (x[0], x[1], x[2], x[3], x[4], x[5], x[6], x[7], x[8]))
 
    return ans_list
 
 
# 打印列表
def print_ans_list(ans_list):
    for item_list in ans_list:
        for item in item_list:
            print(item, end=" ")
        print()
 
 
if __name__ == '__main__':
    sudoku()

import math
 
# 求解九宫格
def sudoku():
    nums_list = input().split(" ")
    nums_list = [int(num) for num in nums_list]
 
    mul_item, d_list = search_tuple(nums_list)
    ans_list = arrange_tuple(mul_item, d_list)
    ans_list = sort_ans_list(ans_list)
    print_ans_list(ans_list)
 
 
# 求解幻积并统计符合规律的每个数字出现的次数
def search_tuple(nums_list):
    # 求解幻积
    total_mul = 1
    for num in nums_list:
        total_mul *= num
 
    # 幻积
    mul_item = round(math.pow(total_mul, (1 / 3)))
    nums_list.sort()
 
    # 寻找符合幻积的数字对,并统计每个数字出现的次数
    d = {}
    length = len(nums_list)
    for i in range(length - 2):
        for j in range(i + 1, length - 1):
            for k in range(j + 1, length):
                if nums_list[i] * nums_list[j] * nums_list[k] == mul_item:
                    d[nums_list[i]] = d[nums_list[i]] + 1 if nums_list[i] in d.keys() else 1
                    d[nums_list[j]] = d[nums_list[j]] + 1 if nums_list[j] in d.keys() else 1
                    d[nums_list[k]] = d[nums_list[k]] + 1 if nums_list[k] in d.keys() else 1
 
    d_list = sorted(d.items(), key=lambda x: x[1], reverse=True)
    return mul_item, d_list
 
 
# 根据各个数字出现的次数排列9个数字的顺序
def arrange_tuple(mul_item, d_list):
    ans_list = []
    for i in range(5):
        if i == 2:
            continue
        tmp = [0 for _ in range(9)]
        tmp[4] = d_list[0][0]
        tmp[2 * i] = d_list[1][0]
 
        other_value = 0
        for item in d_list[2: 5]:
            if tmp[2 * i] * tmp[4] * item[0] == mul_item:
                other_value = item[0]
        tmp[8 - 2 * i] = other_value
 
        line_list = []
        for item in d_list[2: 5]:
            if item[0] != tmp[2 * i] and item[0] != tmp[8 - 2 * i]:
                line_list.append(item[0])
 
        if 2 * i == 0 or 2 * i == 8:
            location_list = [2, 6]
        else:
            location_list = [0, 8]
        for j in range(2):
            tmp = tmp.copy()
            if j == 0:
                tmp[location_list[0]] = line_list[0]
                tmp[location_list[1]] = line_list[1]
            else:
                tmp[location_list[0]] = line_list[1]
                tmp[location_list[1]] = line_list[0]
 
            tmp[1] = mul_item // (tmp[0] * tmp[2])
            tmp[3] = mul_item // (tmp[0] * tmp[6])
            tmp[5] = mul_item // (tmp[2] * tmp[8])
            tmp[7] = mul_item // (tmp[6] * tmp[8])
 
            ans_list.append(tmp)
 
    return ans_list
 
 
# 排序
def sort_ans_list(ans_list):
    ans_list.sort(key=lambda x: (x[0], x[1], x[2], x[3], x[4], x[5], x[6], x[7], x[8]))
 
    return ans_list
 
 
# 打印列表
def print_ans_list(ans_list):
    for item_list in ans_list:
        for item in item_list:
            print(item, end=" ")
        print()
 
 
if __name__ == '__main__':
    sudoku()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用jQuery实现九宫格抽奖的示例代码: HTML代码: ```html <div class="lottery"> <ul> <li><a href="#"><img src="img/1.png"></a></li> <li><a href="#"><img src="img/2.png"></a></li> <li><a href="#"><img src="img/3.png"></a></li> <li><a href="#"><img src="img/4.png"></a></li> <li><a href="#"><img src="img/5.png"></a></li> <li><a href="#"><img src="img/6.png"></a></li> <li><a href="#"><img src="img/7.png"></a></li> <li><a href="#"><img src="img/8.png"></a></li> <li><a href="#"><img src="img/9.png"></a></li> </ul> <a href="#" class="btn">开始抽奖</a> </div> ``` CSS代码: ```css .lottery { width: 500px; margin: 0 auto; text-align: center; } .lottery ul { list-style: none; margin: 0; padding: 0; display: inline-block; } .lottery ul li { float: left; margin-right: 10px; } .lottery ul li:last-child { margin-right: 0; } .lottery ul li a { display: block; width: 150px; height: 150px; border: 1px solid #ccc; text-align: center; line-height: 150px; font-size: 24px; color: #333; text-decoration: none; } .lottery ul li a img { max-width: 100%; max-height: 100%; } .lottery .btn { display: inline-block; margin-top: 20px; padding: 10px 20px; font-size: 24px; color: #fff; background-color: #f00; border-radius: 5px; text-decoration: none; } ``` JavaScript代码: ```javascript $(function() { var lottery = { index: -1, // 当前转动到哪个位置,起点位置 count: 0, // 总共有多少个位置 timer: 0, // setTimeout的ID,用clearTimeout清除 speed: 20, // 初始转动速度 times: 0, // 转动次数 cycle: 50, // 转动基本次数:即至少需要转动多少次再进入抽奖环节 prize: -1, // 中奖位置 init: function(id) { if ($("#" + id).find(".lottery-unit").length > 0) { $lottery = $("#" + id); $units = $lottery.find(".lottery-unit"); this.obj = $lottery; this.count = $units.length; $lottery.find(".lottery-unit-" + this.index).addClass("active"); } }, roll: function() { var index = this.index; var count = this.count; var lottery = this.obj; $(lottery).find(".lottery-unit-" + index).removeClass("active"); index += 1; if (index > count - 1) { index = 0; } $(lottery).find(".lottery-unit-" + index).addClass("active"); this.index = index; return false; }, stop: function(index) { this.prize = index; return false; } }; function roll() { lottery.times += 1; lottery.roll(); if (lottery.times > lottery.cycle + 10 && lottery.prize == lottery.index) { clearTimeout(lottery.timer); lottery.prize = -1; lottery.times = 0; click = false; } else { if (lottery.times < lottery.cycle) { lottery.speed -= 10; } else if (lottery.times == lottery.cycle) { var index = Math.random() * (lottery.count) | 0; lottery.prize = index; } else { if (lottery.times > lottery.cycle + 10 && ((lottery.prize == 0 && lottery.index == 7) || lottery.prize == lottery.index + 1)) { lottery.speed += 110; } else { lottery.speed += 20; } } if (lottery.speed < 40) { lottery.speed = 40; } lottery.timer = setTimeout(roll, lottery.speed); } return false; } var click = false; $(document).ready(function() { lottery.init('lottery'); $(".btn").click(function() { if (click) { return false; } else { lottery.speed = 100; roll(); click = true; return false; } }); }); }); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值