洛谷题单2-P5712 【深基3.例4】Apples-python-流程图重构

题目描述

小 B 喜欢吃苹果。她今天吃掉了 x x x 个苹果。英语课上学到了 apple 这个词语,想用它来造句。如果她吃了 1 个苹果,就输出 Today, I ate 1 apple.;如果她没有吃,那么就把 1 换成 0;如果她吃了不止一个苹果,别忘了 apple 这个单词后面要加上代表复数的 s。你能帮她完成这个句子吗?

输入格式

输入一行一个自然数 x x x,表示吃掉的苹果数。

输出格式

根据题目要求输出。

输入输出样例

输入

复制代码
1

输出

复制代码
Today, I ate 1 apple.

方式

代码

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

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

    @staticmethod
    def solution(x):
        if x == 1 or x == 0:
            print(f"Today, I ate {x} apple.")
        else:
            print(f"Today, I ate {x} apples.")


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

if __name__ == '__main__':
    '''用oi_test()接受数据,然后传入solution'''
    x = oi_test()
    # x = oi_input()
    solution(x)

流程图

是 否 开始 主函数调用 获取输入x x == 0 或 1? 格式化单数句子
Today, I ate {x} apple. 格式化复数句子
Today, I ate {x} apples. 打印输出 结束

相关推荐
地平线开发者11 小时前
【模型轻量化专题】衡量模型轻量性的指标
算法
用户77833661321112 小时前
用 React Hook 封装搜索数据:useSerp 的防抖、缓存与错误处理
python·api
学习星球14 小时前
OFDM技术精讲:正交性推导、循环前缀原理与80行Python链路仿真(附实测数据)
算法·面试·前端框架
65岁退休Coder16 小时前
LangGraph v1.2.9 节点容错策略 & 流式输出 & 持久化记忆管理
后端·python·langchain
ikun_文18 小时前
Django框架路由Router的使用
python·pycharm·django
IvanCodes19 小时前
Python 基础语法(二):字符串与常用操作
python
昭昭日月明19 小时前
LangChain 生态:从链到代理,开发者需要掌握的三大核心
python·langchain·agent
Csvn19 小时前
🐍 Day 8:面向对象编程
后端·python
程序员天天困19 小时前
向量检索不准怎么办:混合检索与 Rerank 重排序召回优化实战
后端·python·ai编程
alphaTao20 小时前
LeetCode 每日一题 2026/8/24-2026/8/30
python·算法·leetcode