码迷,mamicode.com
首页 > 其他好文 > 详细

平分七筐鱼

时间:2019-11-13 22:16:11      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:static   sys   strong   算法   inf   sum   png   list   col   

平分七框鱼

问题描述

甲乙丙三位渔夫出海打鱼,他们随船带了21只箩筐,当晚返航时,他们发现有7筐装满了鱼,还有7筐只装了半筐鱼,另外7筐则是空的. 由于他们没有秤,只好通过目测认为7个满筐鱼的重量是相等的,7个半筐鱼的重量也是相等的
在不将鱼倒出来的前提下,怎么将鱼和筐平分为三份?

算法思路

技术图片

代码示例

Python

fishman_list = [[0] * 3 for x in range(3)]


def fill_list():
    # 如果没有这个数量关系的话 => 其实是9重循环

    # x:甲分到的满筐数
    for x in range(0, 8):
        if (x > 3): continue
        fishman_list[0][0] = x
        # 通过满框可以计算半筐和空筐
        fishman_list[0][1] = int((3.5 - x) / 0.5)
        fishman_list[0][2] = 7 - fishman_list[0][0] - fishman_list[0][1]

        # y:乙分到的满筐数
        for y in range(0, 8 - x):
            if (y > 3): continue
            fishman_list[1][0] = y
            # 通过满框可以计算半筐和空筐
            fishman_list[1][1] = int((3.5 - y) / 0.5)
            fishman_list[1][2] = 7 - fishman_list[1][0] - fishman_list[1][1]

            # z:丙分到的满筐数
            for z in range(0, 8 - x - y):
                if (z > 3): continue
                fishman_list[2][0] = z
                # 通过满框可以计算半筐和空筐
                fishman_list[2][1] = int((3.5 - z) / 0.5)
                fishman_list[2][2] = 7 - fishman_list[2][0] - fishman_list[2][1]

                if (judge()):
                    print_list()


def judge():
    col1 = fishman_list[0][0] + fishman_list[1][0] + fishman_list[2][0]
    col2 = fishman_list[0][1] + fishman_list[1][1] + fishman_list[2][1]
    col3 = fishman_list[0][2] + fishman_list[1][2] + fishman_list[2][2]
    if (col1 != 7 or col2 != 7 or col3 != 7):
        return False

    # fish: 鱼的重量
    for list_item in fishman_list:
        fish = list_item[0] + list_item[1] * 0.5
        if (not sum(list_item) == 7 and fish == 3.5):
            return False

    return True


def print_list():
    for list_item in fishman_list:
        for y in list_item:
            print("%2d" % y, end=" ")
        print()
    print()


fill_list()

Java


public class 平分七筐鱼 {

    static boolean judge(int[][] fishman) {

        int col1 = fishman[0][0] + fishman[1][0] + fishman[2][0];
        int col2 = fishman[0][1] + fishman[1][1] + fishman[2][1];
        int col3 = fishman[0][2] + fishman[1][2] + fishman[2][2];
        if (!(col1 == 7 && col2 == 7 && col3 == 7))
            return false;

        return true;
    }

    static void fill_arrs() {
        // a: 满筐 b: 半筐 c: 空筐
        // 1: 甲 2:乙 3: 丙
        int[][] fishman = null;
        for (int a1 = 0; a1 <= 7 && a1 < 4; a1++) {

            int b1 = (int) ((3.5 - a1) / 0.5);
            int c1 = 7 - a1 - b1;
            for (int a2 = 0; a2 <= 7 - a1 && a2 < 4; a2++) {

                int b2 = (int) ((3.5 - a2) / 0.5);
                int c2 = 7 - a2 - b2;
                for (int a3 = 0; a3 <= 7 - a1 - a2 && a3 < 4; a3++) {

                    int b3 = (int) ((3.5 - a3) / 0.5);
                    int c3 = 7 - a3 - b3;
                    fishman = new int[][] { { a1, b1, c1 }, { a2, b2, c2 }, { a3, b3, c3 } };
                    if (judge(fishman))
                        print_arrs(fishman);
                }
            }
        }

    }

    static void print_arrs(int[][] fishman) {
        for (int i = 0; i < fishman.length; i++) {
            for (int j = 0; j < fishman[0].length; j++) {
                System.out.printf("%2d", fishman[i][j]);
            }
            System.out.println();
        }
        System.out.println();
    }

    public static void main(String[] args) {
        fill_arrs();
    }

}

平分七筐鱼

标签:static   sys   strong   算法   inf   sum   png   list   col   

原文地址:https://www.cnblogs.com/Rowry/p/11853400.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!