4073 找规律输出(模拟)

1. 问题描述:

给定一个整数 n,参考给定样例寻找规律,并输出一个字符串。提示: 观察样例可以发现,I hate that和I love that交替出现 n−1 次,最后再出现一次I hate it或I love it。

输入格式

一个整数 n。

输出格式

一行,一个字符串。

数据范围

前 5 个测试点满足 1 ≤ n ≤ 5。
所有测试点满足 1 ≤ n ≤ 100。

输入样例1:

1

输出样例1:

I hate it

输入样例2:

2

输出样例2:

I hate that I love it

输入样例3:

3

输出样例3:

I hate that I love that I hate it

输入样例4:

4

输出样例4:

I hate that I love that I hate that I love it

输入样例5:

5

输出样例5:

I hate that I love that I hate that I love that I hate it
来源:https://www.acwing.com/problem/content/4076/

2. 思路分析:

分析题目可以知道模拟整个过程即可。

3. 代码如下:

class Solution:
    def process(self):
        n = int(input())
        s1, s2, s3, s4 = "I hate it", "I love it", "I hate that", "I love that"
        for i in range(n):
            if i == n - 1 and i % 2 == 0:
                print(s1)
            elif i == n - 1 and i % 2 == 1:
                print(s2)
            elif i % 2 == 0:
                print(s3, end=" ")
            else:
                print(s4, end=" ")


if __name__ == '__main__':
    Solution().process()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值