鱼C论坛

 找回密码
 立即注册
查看: 3298|回复: 6

韩信点兵问题

[复制链接]
发表于 2022-10-25 09:34:14 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
韩信有一队兵,他想知道有多少人,便让士兵排队报数:按从1至5报数,最末一个士兵报的数为1;按从1至6报数,最末一个士兵报的数为5;按从1至7报数,最末一个士兵报的数为4;最后再按从1至11报数,最末一个士兵报的数为10。编程求韩信至少有多少兵?用for循环怎么搞
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-10-25 10:09:07 | 显示全部楼层
n = 1
while True:
    if n % 5 == 1 and n % 6 == 5 and n % 7 == 4 and n % 11 == 10:
        print(n)
        break
    n += 1
        运行实况:
D:\[00.Exerciese.2022]\Python>python x.py
2111

D:\[00.Exerciese.2022]\Python>
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-10-25 10:14:55 | 显示全部楼层
答案是多少?
sh-5.1$ cat main.py
#!/usr/bin/env python
#coding=utf-8

count = 0
while True:
    a = (count % 5) + 1
    b = (count % 6) + 1
    c = (count % 7) + 1
    d = (count % 11) + 1
    count += 1
    if a != 1: continue
    if b != 5: continue
    if c != 4: continue
    if d != 10: continue
    print(count)
    break
sh-5.1$ ./main.py
2111
sh-5.1$
sh-5.1$ cat main.py
#!/usr/bin/env python
#coding=utf-8

from itertools import repeat

count = 0
for _ in repeat(None):
    a = (count % 5) + 1
    b = (count % 6) + 1
    c = (count % 7) + 1
    d = (count % 11) + 1
    count += 1
    if a != 1: continue
    if b != 5: continue
    if c != 4: continue
    if d != 10: continue
    print(count)
    break
sh-5.1$ ./main.py
2111
sh-5.1$
sh-5.1$ cat main.py
#!/usr/bin/env python
#coding=utf-8

from itertools import count

for i in count():
    a = (i % 5) + 1
    b = (i % 6) + 1
    c = (i % 7) + 1
    d = (i % 11) + 1
    if a != 1: continue
    if b != 5: continue
    if c != 4: continue
    if d != 10: continue
    print(i)
    break
sh-5.1$ ./main.py
2110
sh-5.1$
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-10-25 10:15:29 | 显示全部楼层
2110  是吗?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-10-25 10:18:13 | 显示全部楼层
sh-5.1$ cat main.py
#!/usr/bin/env python
#coding=utf-8

count = -1
while True:
    count += 1
    a = (count % 5) + 1
    b = (count % 6) + 1
    c = (count % 7) + 1
    d = (count % 11) + 1
    if a != 1: continue
    if b != 5: continue
    if c != 4: continue
    if d != 10: continue
    print(count)
    break
sh-5.1$ ./main.py
2110
sh-5.1$
sh-5.1$ cat main.py
#!/usr/bin/env python
#coding=utf-8

from itertools import repeat

count = -1
for _ in repeat(None):
    count += 1
    a = (count % 5) + 1
    b = (count % 6) + 1
    c = (count % 7) + 1
    d = (count % 11) + 1
    if a != 1: continue
    if b != 5: continue
    if c != 4: continue
    if d != 10: continue
    print(count)
    break
sh-5.1$ ./main.py
2110
sh-5.1$
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-10-25 10:20:15 | 显示全部楼层
这个版本的最好,我最喜欢
sh-5.1$ cat main.py
#!/usr/bin/env python
#coding=utf-8

from itertools import count

for i in count():
    a = (i % 5) + 1
    b = (i % 6) + 1
    c = (i % 7) + 1
    d = (i % 11) + 1
    if a != 1: continue
    if b != 5: continue
    if c != 4: continue
    if d != 10: continue
    print(i)
    break
sh-5.1$ ./main.py
2110
sh-5.1$
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2022-10-25 10:24:41 | 显示全部楼层
答案是 2111
sh-5.1$ cat main.py
#!/usr/bin/env python
#coding=utf-8

from itertools import count

for i in count():
    a = (i % 5) + 1
    b = (i % 6) + 1
    c = (i % 7) + 1
    d = (i % 11) + 1
    if a != 1: continue
    if b != 5: continue
    if c != 4: continue
    if d != 10: continue
    print(i + 1)
    break
sh-5.1$ ./main.py
2111
sh-5.1$
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-25 23:21

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表