韩信点兵问题
韩信有一队兵,他想知道有多少人,便让士兵排队报数:按从1至5报数,最末一个士兵报的数为1;按从1至6报数,最末一个士兵报的数为5;按从1至7报数,最末一个士兵报的数为4;最后再按从1至11报数,最末一个士兵报的数为10。编程求韩信至少有多少兵?用for循环怎么搞{:10_266:} n = 1while True:
if n % 5 == 1 and n % 6 == 5 and n % 7 == 4 and n % 11 == 10:
print(n)
break
n += 1
运行实况:
D:\\Python>python x.py
2111
D:\\Python> 答案是多少?
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$
2110是吗?
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$
这个版本的最好,我最喜欢
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$
答案是 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$
页:
[1]