Codeforces 705A Hulk Solution & Explanation

Difficulty : 800

Problem Description

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.

Input

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

Output

Print Dr.Banner’s feeling in one line.

Examples

Input1
OutputI hate it
Input2
OutputI hate that I love it
Input3
OutputI hate that I love that I hate it

Solution

Our code efficiently constructs Hulk’s feelings using a for loop and ternary operator.

The use of StringBuilder optimizes the concatenation of strings, providing a more efficient way to build the final result.

C# Solution

Solution1

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 Solution

Solution1

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());
    }
}

Python3 Solution

Solution1

n = int(input())
result = ""

for i in range(n-1):
    result+=("I hate that " if i%2==0 else "I love that ")
    
result+=("I love it" if n%2==0 else "I hate it")

print(result)

Conclusion

🧡If my solution helps, that is my honor!

🧡You can support me by sharing my posts, thanks you~~

✅If you got any problem about the explanation or you need other programming language solution, please feel free to let me know !!

The problem link : Problem – 705A – Codeforces

Random Codeforces Posts

Leave a Reply

Your email address will not be published. Required fields are marked *