How the Wizard Fought the Monster – A Number Story
A story of a wizard who fought a monster made of numbers
Once upon a time in the magical land of Integers,
a wise wizard had to defeat a monster made of numbers!
The monster’s power was just a number – but a very stubborn one. ๐ฎ
To win, the wizard had to bring the number down to zero.
๐งพ The Monster's Rule
-
If the number is even → use Magic Attack → divide it by 2
-
If the number is odd → use Basic Attack → subtract 1
Let’s say the monster’s number is 14.
Here’s how the battle goes:
Step 0: Monster = 14 (even) → Magic Attack → 7
Step 1: Monster = 7 (odd) → Basic Attack → 6
Step 2: Monster = 6 (even) → Magic Attack → 3
Step 3: Monster = 3 (odd) → Basic Attack → 2
Step 4: Monster = 2 (even) → Magic Attack → 1
Step 5: Monster = 1 (odd) → Basic Attack → 0
๐ป Wizard's Code in Python
def number_of_steps(num):
steps = 0
while num > 0:
if num % 2 == 0:
num = num // 2 # Magic attack (even)
else:
num -= 1 # Basic attack (odd)
steps += 1
return steps
๐ What it does (in simple words):
Start with a number.
If it’s even, divide it by 2.
If it’s odd, subtract 1.
Count how many steps it takes to get to 0.
๐ป Wizard's Code in C#
public class Solution {
public int NumberOfSteps(int num) {
int steps = 0;
while (num > 0) {
if (num % 2 == 0)
num /= 2; // Magic attack
else
num -= 1; // Basic attack
steps++;
}
return steps;
}
}
๐ฎ Turn This Into a Game!
This logic can be a fun game mechanic:
-
๐ Monster's HP is a number
-
✨ Even HP → Magic Attack (costs mana, powerful)
-
๐ก️ Odd HP → Basic Attack (free, but weak)
-
⏳ Goal: Defeat monster with minimum steps or in limited time!
Players need to think ahead and pick the smartest move based on the number!
Code inspired by algorithm problem #1342 – Number of Steps to Reduce a Number to Zero
Reference: 1342. Number of Steps to Reduce a Number to Zero @LeetCode
๐ Python Full Version (with Input & Output)
๐ป C# Full Version (Console Application Style)
Comments
Post a Comment