A - Stage Clear Editorial by en_translator
For beginners
In this problem, you are mainly asked to:
- obtain which stage in which world he is currently in based on the information given in the input, and
- find which stage in which world the next stage is (using conditional branches appropriately).
There are several approaches for the former. For example, the following approaches allow us to obtain the information of the world and stage as integers.
- Receive the entire input as a string, and interpret the first and last character as integers: this can be done by receiving three-letter string, obtaining a character at a specific position, and converting a character to a digit, which can be achieved in various languages.
- Use an input method for which we can specify the input format: the
scanf
function available in C and C++ is capable of reading integers or floating-point numbers even when the input is not whitespace-separated, by providing input format appropriately. In our case, we can writescanf("%d-%d", &a, &b)
to read the desirable values into the variablesa
andb
. - Use an input method that reads as far as interpretable: when C++’s
std::cin
reads an integer, it reads the input as far as it is interpretable as an integer. Once it encounters an input that cannot be interpreted as an integer, it confirms the current value without consuming that. By this behavior, when it tries to read an integer and given4-2
, it first reads4
and reports \(4\), leaving-2
unconsumed. Now we can read one more character, and read another integer to obtain desired values. Alternatively, although this is a bit tricky, but we can successively read two integers from the input, with the latter value multiplied by \(-1\); this can be also used to solve this problem.
For the latter, we can solve it by using an if
statement appropriately.
The following is sample code. We provide several implementation examples with different approaches.
s = input()
a = int(s[0]) # Interpret the first character as an integer
b = int(s[2]) # Interpret the last character as an integer
if b == 8: # If this is the last stage of a world
print(str(a + 1) + '-1') # Report the first stage in the next world
else: # Otherwise
print(str(a) + '-' + str(b + 1)) # Report the next stage in the same world
a, b = map(int, input().split('-')) # Split the input by the character `-` andinterpret each of them as an integer
b += 1 # Advance the stage by one
if b > 8: # If the stage number exceeds 8,
a += 1 # advance to the next world,
b = 1 # and reset the stage number
print(str(a) + '-' + str(b))
#include <iostream>
using namespace std;
int main() {
string S;
cin >> S;
if (S[2] == '8') { // If the last character is 8,
S[0] += 1; // increment the first character 1,
S[2] = '1'; // and set the last character to 1
} else { // Otherwise,
S[2] += 1; // increment the last character by 1
}
cout << S << endl;
}
#include <cstdio>
using namespace std;
int main() {
int a, b;
scanf("%d-%d", &a, &b); // Read two integers from standard input with the specified format
++b; // Try to advance the stage
if (b > 8) { // If the stage number exceeds 8,
++a; // advance the world,
b = 1; // and reset the stage
}
printf("%d-%d\n", a, b); // Print with specified format
return 0;
}
posted:
last update: