您如何使用循环来获取数字的最低有效位?

我对问题要求我在这里做什么感到有些困惑 —-> ( https://i.stack.imgur.com/TSfHH.jpg )

http://img1.mukewang.com/61a5d9020001bb6c07621291.jpg

这是使用python,规则是:只能使用循环和条件


吃鸡游戏
浏览 165回答 1
1回答

偶然的你

以下是您的问题的两种解决方案。第一个是使用递归,第二个是计数器。def contains_two_fives_loop(n):&nbsp; &nbsp; """Using loop."""&nbsp; &nbsp; counter = 0&nbsp; &nbsp; while n:&nbsp; &nbsp; &nbsp; &nbsp; counter += n % 10 == 5&nbsp; &nbsp; &nbsp; &nbsp; n //= 10&nbsp; &nbsp; return 2 <= counterdef contains_two_fives_recursion(n):&nbsp; &nbsp; """Using recursion."""&nbsp; &nbsp; return 2 <= (n % 10) == 5 + contains_two_fives(n // 10)def contains_two_fives_str_counter(n):&nbsp; &nbsp; """Convert to string and count 5s in the string."""&nbsp; &nbsp; return 2 <= str(n).count("5")
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python