Codeforces 705A Hulk 浩克

難易度 : 800

英文原文如下

Dr. Bruce Banner hates his enemies (like others don’t). As we all know, he can barely talk when he turns into the incredible Hulk. That’s why he asked you to help him to express his feelings.

Hulk likes the Inception so much, and like that his feelings are complicated. They have n layers. The first layer is hate, second one is love, third one is hate and so on…

For example if n = 1, then his feeling is “I hate it” or if n = 2 it’s “I hate that I love it”, and if n = 3 it’s “I hate that I love that I hate it” and so on.

Please help Dr. Banner.

中文翻譯

布魯斯·班納博士與其他人不同,他極度厭惡他的敵人。但我們都知道,他當變成無敵浩克的時候,他幾乎無法說話。這就是為什麼他請求你來幫他表示他的感受。

浩克非常喜歡全面啟動 這一部電影,就像他的感受一樣複雜。他的感受有n層。第一層是恨,第二層是愛,第三層是恨,以此類推…

例如,如果n = 1,那麼他的感受是”I hate it”;如果n = 2,那麼他的感受是”I hate that I love it”;如果n = 3,那麼他的感受是”I hate that I love that I hate it”,以此類推。

請幫助布魯斯博士。

輸入

The only line of the input contains a single integer n (1 ≤ n ≤ 100) — the number of layers of love and hate.

唯一一行的輸入包含了一個整數 n (1 ≤ n ≤ 100) — 愛跟恨共有幾層。

輸出

Print Dr.Banner’s feeling in one line.

在一行中印出班納博士的感受。

範例

輸入1
輸出I hate it
輸入2
輸出I hate that I love it
輸入3
輸出I hate that I love that I hate it

解題思路

從題目中,我們可以發現一定是從 I hate 開頭,然後不斷交換,最後一次結尾則不是that而是it。

故,我們可以透過迴圈搭配簡單的三元不等式來判斷目前所在層數

然後一如往常,使用了StringBuilder 而不是string來提高部分字串效能

C#解決方案

方案1

using System.Text;

int n = int.Parse(Console.ReadLine());
var result = new StringBuilder();

for(int i=0; i<n-1; i++){
    result.Append(i%2==0?"I hate that ":"I love that ");
}

result.Append(n%2==0?"I love it":"I hate it");

Console.WriteLine(result.ToString());

Java解決方案

方案1

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        int n = scanner.nextInt();
        StringBuilder result = new StringBuilder();
        
        for(int i=0; i<n-1; i++){
            result.append(i%2==0?"I hate that ":"I love that ");
        }
        
        result.append(n%2==0?"I love it":"I hate it");
        
        System.out.println(result.toString());
    }
}

結論

看到題目時,想說怎麼這麼有趣哈哈,不只出現綠巨人浩克還出現了全面啟動ㄟ~

話說回來,全面啟動最後的陀螺到底停下來了沒…該不會至始至終都沒離開過夢境的嗎 !?

🧡如果文章有幫上你的忙,那是我的榮幸

🧡收藏文章或幫我點個廣告,那都是對我的支持

✅如有任何疑問,歡迎透過留言或messenger讓我知道 !

題目連結 : Problem – 705A – Codeforces

一些其他的Codeforces文章

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *