注册 登录
编程论坛 数据结构与算法

蛮力法如何求鸡和兔的个数

yang158 发布于 2020-04-19 19:19, 5112 次点击
有一群鸡和一群兔,它们的只数相同,它们的脚数都是三位数,且这两个三位数的数字分别是0,1,2,3,4,5。设计一个算法用蛮力法求鸡和兔各有多少只,?它们的脚数各是多少
5 回复
#2
fulltimelink2020-05-23 23:30
102/2=51可知只数>=51
542/4=135可知只数<=135
循环只数从51到135 计算鸡脚数A和兔脚数B   
判断A*1000+B是否有重复数字即可
#3
qrezhy2021-04-30 14:15
只有本站会员才能查看附件,请 登录
只有本站会员才能查看附件,请 登录
#4
傻眼猫咪2021-08-29 00:24
程序代码:
from itertools import permutations
feet = permutations([0, 1, 2, 3, 4, 5], 3)
feet = list(set([int(''.join([str(j) for j in i])) for i in feet if len(i) == 3 and i[0] != 0]))
for chicken in feet:
    for rabbit in feet:
        if chicken*2 == rabbit and (chicken%2 == 0):
            if len(set(str(chicken))^set(str(rabbit))) == 6:
                print(f"雞 {chicken>>1} 只,{chicken} 只腳\n兔 {rabbit>>2} 只,{rabbit } 只腳")

輸出:
76 只,152 只腳
76 只,304 只腳




[此贴子已经被作者于2021-8-29 00:25编辑过]

#5
diycai2021-09-09 16:39
回复 4楼 傻眼猫咪

void main()
{
    int i, j, k;
    int v1, v2, tmp;

    for (i=1; i<3; i++)
    {
        for (j=0; j<6; j++)
        {
            for (k=0; k<6; k++)
            {
                v1 = i*100 + j*10 + k;
                v2 = v1*2;
                tmp = (1<<i) | (1<<j) | (1<<k);
                tmp |= (1<<(v2/100)) | (1<<((v2/10)%10)) | (1<<(v2%10));

                if ((v1 & 1)==0 && tmp==0x3F)
                {
                    printf("%d %d\n", v1, v2);
                }
            }
        }
    }
}

[此贴子已经被作者于2021-9-9 16:55编辑过]

#6
mrexcel2022-12-17 22:10
for chick in range(102,255,2):
    rabbit=2*chick
    if all((str(chick)+str(rabbit)).count(d) for  d in "012345"):
        print(f"鸡 {chick//2} 只,{chick} 只脚;兔 {rabbit//4} 只,{rabbit} 只脚")


[此贴子已经被作者于2022-12-17 22:13编辑过]

1