In a magical land, there are rabbits that only eat carrots, and they eat one carrot per day. There are four types of carrots: red, white, yellow, and moldy (black). Eating these carrots causes different changes in the rabbit's weight: Eating a red carrot makes the rabbit gain x grams. Eating a white carrot makes the rabbit gain y grams. Eating a yellow carrot makes the rabbit lose z grams (pickled yellow carrots are unappetizing…). Eating a moldy carrot makes the rabbit lose w grams (with the additional effect: poisoning…). Now, given x, y, z, and w, you need to determine the rabbit's weight after a certain number of days!
P.S. Poisoning causes the rabbit to lose n grams each day (not counting the day of poisoning), and the poisoning effect can accumulate. m is the rabbit's initial weight. The rabbit gets poisoned in the morning and eats food in the evening.
Sample Inputs/Outputs
Sample Input(s) | Sample Output(s) |
---|---|
The first line contains the number of test cases. For each test case: The first line contains x, y, z, w, n, and m. The second line is a sequence of numbers where: 1 represents a red carrot, 2 represents a white carrot, 3 represents a yellow carrot, 4 represents a black carrot, 0 represents no food. This line indicates what the rabbit eats over the given period. | You need to simulate the weight change for each day, considering the accumulated poisoning effects. The goal is to determine the rabbit's weight after following the sequence of food intake. If the rabbit's weight drops to or below 0 at any point, output "bye~Rabbit". Otherwise, output the rabbit's final weight at the end of the period. |
4 5 3 2 4 3 10 1 1 2 3 3 3 3 4 3 3 5 3 2 4 3 10 1 1 2 3 3 3 3 4 3 3 2 2 2 2 2 2 2 5 3 2 4 3 10 4 1 3 3 1 1 2 2 1 1 3 1 1 1 1 4 10 3 2 2 1 5 1 4 4 0 0 4 1 2 2 2 0 0 2 2 0 | 1g bye~Rabbit bye~Rabbit bye~Rabbit |
Thought Process
Use a string to store the second line of each test case. Then, use a for loop to store the non-whitespace characters into an array (since the integers in the second line are all single digits). Afterward, use another for loop to determine the weight changes of the rabbit. A more advanced method can use StringStream to solve the problem. Note that after using cin to read the six integers, you need to do a meaningless get line once; otherwise, the getline will fail. Alternatively, you can use cin.ignore(). To avoid TLE (Time Limit Exceeded), when determining the weight changes, if it is already found that the rabbit's weight is less than 0, you can directly break the for loop and use a boolean to determine whether to output the rabbit's current weight.
Sample Code-ZeroJudge A271: Colored Radish
#include <iostream>
#include <string>
using namespace std;
int main() {
int N;
int red, white, yellow, black, poison, current;
cin >> N;
string str;
for (int i = 0; i<N; i++)
{
cin >> red >> white >> yellow >> black >> poison >> current;
getline(cin , str);
getline(cin, str);
int carrots[10000] = {};
int tmp = 0;
int count = 0;
bool dead = false;
for (int j = 0; j<str.length(); j++)
{
if (str[j] != ' ')
{
carrots[tmp] = str[j] - '0';
tmp += 1;
}
}
for (int i = 0; i<tmp; i++)
{
current -= count * poison;
if (current <= 0)
{
cout << "bye~Rabbit" << endl;
dead = true;
break;
}
if (carrots[i] == 1)
{
current += red;
}
else if (carrots[i] == 2)
{
current += white;
}
else if (carrots[i] == 3)
{
current -= yellow;
}
else if (carrots[i] == 4)
{
current -= black;
count += 1;
}
if (current <= 0)
{
cout << "bye~Rabbit" << endl;
dead = true;
break;
}
}
if (!dead)
{
cout << current << "g" << endl;
}
}
}
//ZeroJudge A271
//Dr. SeanXD