# | Problem | Pass Rate (passed user / total user) |
---|---|---|
12109 | Text Editor 2 |
|
13793 | Pokemon Battle Manager 2 |
|
Description
In this problem we simulate a simple text editor. Given two lines of keyboard input texts and commands, output the final text content.
The text editing rules are defined as following:
1.
Normal alphabetical input and whitespace input (abcdefg…. and ‘ ‘) in the first line.
And a series of two special commands started with a backslash(/) character in the second line.
2.
The backspace command which deletes a letter before the cursor (/b)
3.
The navigating command which move the cursor to the right(/r)
4.
The cursor is at the beginning of the text initially.
For example, for the following input:
The quick brown fox jumps over the lazy dog
/b/r/r/r/b
You should first store the "The quick brown fox jumps over the lazy dog" in the input char array and set cursor=0.
Then you can simulate the operation of the text editor.
The size of the text content is less than or equal to 500 characters, and there will be less than or equal to 500 commands when simulating.
The C library fgets
function reads a string from the input stream
argument and stores it in str
. fgets
reads characters from the current stream position to and including the first newline character, to the end of the stream, or until the number of characters read is equal to numChars
- 1, whichever comes first. The result stored in str
is appended with a null character. The newline character, if read, is included in the string.
Hint:
#include <stdio.h> #define MAX_SIZE 1005 char content[MAX_SIZE]; char input[MAX_SIZE]; char command[MAX_SIZE]; int main() { fgets(input, MAX_SIZE, stdin); fgets(command, MAX_SIZE, stdin); /* your code here */ printf("%s", content); return 0; }
Input
The first line is the text content which should be edited.
The second line is the commands.
There is always a valid command(/b /r) right after the backslash character.
Output
The final text content, and there should be no '\n' at the end of the output.
Sample Input Download
Sample Output Download
Tags
Discuss
Description
A long long time ago, the sky was dark and gloomy, the ocean was deep and mysterious, and the continent was covered by lava and sandstorm. The mad wild world was dominated by what’s called Pokémon today. It’s a prehistoric era, where the ancient monsters were both arrogant and noble. But it will only last until the human–or whatever the so-called trainer is–comes.
These beautiful creatures are now puppets of a bunch of goddamn ten years old kids. They travel across countries to collect different species of Pokémon for their fetishism, for they are addicted to control, to dominate lives that cannot defy them. Thousands of thousands of innocent Pokémons got caught, imprisoned, and traded as goods.
And yet it’s not the worst. Like any civilization did to the slaves, trainers love to have their Pokémon battle against other trainers’ Pokémon. They compete with each other in their Pokémon battles and consider it glorious if they win, although all they do is sit and watch the poor Pokémons bleeding, burning to death.
Now as a Pokémon battle field manager, you are asked to report the battle process between two given Pokémons. We’ll dig into the details in the following.
In this problem, you are asked to implement 2 functions:
- Cure a Pokémon
- Simulate a battle
The following is “function.h”. It is guaranteed that the given Pokémon would only be one of the following 5 types: “Geodude”, “Raichu”, “Golbat”, “Magnemite”, and “Exeggutor”, indexed from 0 to 4.
For the function “recover”, you are going to cure a Pokémon, i.e. set Hp to maxHp.
For the function “battle”, you are going to simulate a battle process. Here are the details:
- The two Pokémon take turns to move. Pokémons can choose to used attack or special attack on their turns. Pokémon A would move first.
- When Pokémon P1 uses attack, it causes damage to the other Pokémon P2, i.e. the Hp of P2 would decrease by the damage. The damage is calculated by the formula below:
damage = max(2 × (P1.attack − P2.defense), 1)
After the damage is applied to P2, if P2’s HP is less than 0, P2’s HP should be set to 0. - When Pokémon P1 uses special attack, it causes damage to the other Pokémon P2, i.e. the Hp of P2 would decrease by the damage. The damage is calculated by the formula below:
damage = max(2 × (P1.special_attack − P2.special_defense), 1)
After the damage is applied to P2, if P2’s HP is less than 0, P2’s HP should be set to 0. - Pokémon would use attack on their first turn. After that, if they use attack on their previous turn, they would use special attack at this turn; if they use special attack on their previous turn, they would use attack at this turn.
- If a Pokémon P1 uses attack to Pokémon P2 on its turns, print the following two sentences: “{P1.name} used {P1.first_move}!”, “{P2.name} now has {P2.Hp} HP.”, each end with a newline character.
- If a Pokémon P1 uses special attack to Pokémon P2 on its turns, print the following two sentences: “{P1.name} used {P1.second_move}!”, “{P2.name} now has {P2.Hp} HP.”, each end with a newline character.
- Whenever any Pokémon has its Hp equal to 0, the battle ends directly, and you have to report the battle result. In the case that the two of them both got 0 Hp in the end, you have to print “Draw.” ends with a newline character. Otherwise, suppose that the winner is P, the loser is L, print “{P.name} is the winner! {L.name} died in vain…” ends with a newline character, too. After that, print a blank line for mourning one more be gone spirit.
#include <stdio.h>
typedef struct pokemon{
int id;
char name[20];
char first_move[20];
char second_move[20];
int attack;
int special_attack;
int defense;
int special_defense;
int Hp;
int maxHp;
}Pokemon;
// TODO: recover the Pokémon
void recover(Pokemon *P);
// TODO: simulate and print the process of the battle
void battle(Pokemon *A, Pokemon *B);
Input
The first line contains an integer n, representing the number of Pokémons.
The second line contains n integers xi, representing the type of each Pokémon.
The third line contains an integer t, representing the number of operations.
Then, t lines follow, each representing an operation.
Each operation is in the following forms:
1 id:
The id th Pokémon recovered.
2 a b:
Please print the battle process of the a th Pokémon and the b th Pokémon.
Output
Execute each operation described in the statement above.
Check the sample output for more details.