洛谷题单3-P1423 小玉在游泳-python-流程图重构

题目描述

小玉开心的在游泳,可是她很快难过的发现,自己的力气不够,游泳好累哦。已知小玉第一步能游 2 2 2 米,可是随着越来越累,力气越来越小,她接下来的每一步都只能游出上一步距离的 98 % 98\% 98%。现在小玉想知道,如果要游到距离 s s s 米的地方,她需要游多少步呢。请你编程解决这个问题。

输入格式

输入一个实数 s s s(单位:米),表示要游的目标距离。

输出格式

输出一个整数,表示小玉一共需要游多少步。

输入输出样例

输入

复制代码
4.3

输出

复制代码
3

说明/提示

数据保证, 0 ≤ s < 100 0 \leq s < 100 0≤s<100,且 s s s 小数点后最多只有一位。

方式

代码

python 复制代码
class Solution:
    @staticmethod
    def oi_input():
        """从标准输入读取数据"""
        s_float = float(input())
        return s_float

    @staticmethod
    def oi_test():
        """提供测试数据"""
        return 4.3

    @staticmethod
    def solution(s_float):
        step, next = 2, 0.98

        sum, count = 0, 0
        while sum < s_float:
            sum += (step * next ** count)
            count += 1

        print(count)


oi_input = Solution.oi_input
oi_test = Solution.oi_test
solution = Solution.solution

if __name__ == '__main__':
    s_float = oi_test()
    # s_float = oi_input()
    solution(s_float)

流程图

是 否 开始 输入目标距离S 初始化sum=0, count=0, step=2, next=0.98 sum < S ? sum += step * next^count count += 1 输出count 结束

相关推荐
Flittly1 小时前
【从零手写 ClaudeCode:learn-claude-code 项目实战笔记】(3)TodoWrite (待办写入)
python·agent
千寻girling6 小时前
一份不可多得的 《 Django 》 零基础入门教程
后端·python·面试
千寻girling6 小时前
Python 是用来做 AI 人工智能 的 , 不适合开发 Web 网站 | 《Web框架》
人工智能·后端·算法
颜酱9 小时前
一步步实现字符串计算器:从「转整数」到「带括号与优化」
javascript·后端·算法
databook9 小时前
探索视觉的边界:用 Manim 重现有趣的知觉错觉
python·动效
明月_清风10 小时前
Python 性能微观世界:列表推导式 vs for 循环
后端·python
明月_清风10 小时前
Python 性能翻身仗:从 O(n) 到 O(1) 的工程实践
后端·python
helloweilei1 天前
python 抽象基类
python
用户8356290780511 天前
Python 实现 PPT 转 HTML
后端·python
CoovallyAIHub1 天前
语音AI Agent编排框架!Pipecat斩获10K+ Star,60+集成开箱即用,亚秒级对话延迟接近真人反应速度!
深度学习·算法·计算机视觉