GESP Level 3 exam questions, string-related problems, difficulty ★★☆☆☆.
luogu-B3926 [GESP202312 Level 3] Unit Conversion
Problem Requirements
Problem Description
Little Yang’s math homework this week is to perform unit conversions. As someone who enjoys programming, Little Yang decided to write a program to help him solve these problems.
Little Yang has only learned about length and weight units, specifically:
Length units include kilometers (
<span>km</span>), meters (<span>m</span>), and millimeters (<span>mm</span>), with the following relationships:。Weight units include kilograms (
<span>kg</span>), grams (<span>g</span>), and milligrams (<span>mg</span>), with the following relationships:。Little Yang’s homework only involves converting larger units to smaller units, meaning his homework will only include the following types of problems: converting meters to millimeters, kilometers to millimeters, kilometers to meters, grams to milligrams, kilograms to milligrams, and kilograms to grams.
Now, please help complete the unit conversion program.
Input Format
The first line of input is an integer indicating the number of problems.
Next, lines follow, each containing a string representing the conversion problem, formatted as unit unit . Here, is a non-negative integer not exceeding , and the units and are the English abbreviations of the two units, ensuring they are either both length units or both weight units, and that Unit 1 is larger than Unit 2.
For example, if the problem requires you to convert to , the input would be
<span>1 km = ? mm</span>.It is guaranteed that 。
Output Format
Output lines, sequentially outputting the answers to all problems. When outputting, you only need to substitute the from the input into the answer, while the rest of the output must be exactly as specified. Since Little Yang’s problems only involve converting larger units to smaller units, and the input is an integer, the answer will also be an integer.
For example, if the problem requires you to convert to , the input would be
<span>1 km = ? mm</span>. Thus, you need to output<span>1 km = 1000000 mm</span>.
Input Output Example #1
Input #1
2
1 km = ? mm
1 m = ? mm
Output #1
1 km = 1000000 mm
1 m = 1000 mm
Input Output Example #2
Input #2
5
100 m = ? mm
1000 km = ? m
20 kg = ? g
200 g = ? mg
0 kg = ? mg
Output #2
100 m = 100000 mm
1000 km = 1000000 m
20 kg = 20000 g
200 g = 200000 mg
0 kg = 0 mg
Problem Analysis
Solution Approach
This problem can be approached in two ways:
Method 1: Use getline to read the entire line and split it
-
Input Handling:
- Read the number of problems N
- Use getline to read each complete conversion expression
- Use the string find function to locate key positions such as spaces and question marks
String Processing:
- Split the number, units, and other information based on key positions
- Use substr to extract the larger and smaller units
- Convert the string representation of the number to an integer
Unit Conversion:
- Determine the conversion method based on the unit type (km/kg, m/g)
- Calculate the result according to the multiplicative relationships between the units
- Output the conversion result in the required format
Method 2: Directly use cin to read by spaces
-
Input Handling:
- Read the number of problems N
- Use cin to automatically split the input by spaces
- Read the number, units, equals sign, question mark, etc.
Unit Conversion:
- Determine the unit type and perform the corresponding calculations
- Handle conversions from kilometers to meters/millimeters and kilograms to grams/milligrams
- Handle conversions from meters to millimeters and grams to milligrams
Formatted Output:
- Concatenate the results according to the required format
- Maintain the original input’s spaces and symbols
Complexity Analysis:
- Time Complexity: O(N), where N is the number of problems
- Space Complexity: O(1), using only constant-level additional space
Example Code
Method 1: Read the entire line and use the string find function to split
#include <iostream>
#include <string>
int main() {
// Read the number of problems
int n;
std::cin >> n;
// Clear the newline character from the input buffer to avoid affecting subsequent getline reads
std::cin.ignore();
// Loop to process each conversion problem
for (int i = 0; i < n; i++) {
std::string str;
// Read a complete line of input
getline(std::cin, str);
// Find key position indices
std::size_t first_blank_idx = str.find(" "); // First space position
std::size_t second_blank_idx = str.find(" ", first_blank_idx + 1); // Second space position
std::size_t question_idx = str.find("?"); // Question mark position
// Extract unit information
std::string big_unit = str.substr(
first_blank_idx + 1, second_blank_idx - first_blank_idx - 1); // Larger unit
std::string small_unit =
str.substr(question_idx + 2, str.length() - question_idx - 2); // Smaller unit
// Extract the value and convert to integer
int first_num = std::stoi(str.substr(0, first_blank_idx));
int second_num;
// Perform conversion calculation based on unit type
if (big_unit == "km" || big_unit == "kg") {
if (small_unit == "m" || small_unit == "g") {
second_num = first_num * 1000; // Convert kilometers to meters or kilograms to grams
} else if (small_unit == "mm" || small_unit == "mg") {
second_num = first_num * 1000 * 1000; // Convert kilometers to millimeters or kilograms to milligrams
}
} else if (big_unit == "m" || big_unit == "g") {
second_num = first_num * 1000; // Convert meters to millimeters or grams to milligrams
}
// Output the result in the required format
std::cout << first_num << " " << big_unit << " " << "=" << " "
<< second_num << " " << small_unit << std::endl;
}
return 0;
}
Method 2: Directly use cin to read, automatically split into 5 segments, simpler
#include <iostream>
#include <string>
int main() {
// Read the number of problems
int n;
std::cin >> n;
while (n--) {
// Define variables to store input data
int f_n, s_n; // f_n: first number, s_n: converted number
std::string b_u, eq, s_u, q; // b_u: larger unit, eq: equals sign, s_u: smaller unit, q: question mark
// Read a line of input data
std::cin >> f_n >> b_u >> eq >> q >> s_u;
// Perform conversion calculation based on unit
if (b_u == "km" || b_u == "kg") { // If it is kilometers or kilograms
if (s_u == "m" || s_u == "g") { // Convert to meters or grams
s_n = f_n * 1000;
} else { // Convert to millimeters or milligrams
s_n = f_n * 1000 * 1000;
}
} else { // If it is meters or grams, convert to millimeters or milligrams
s_n = f_n * 1000;
}
// Output the result in the required format
std::cout << f_n << " " << b_u << " " << "=" << " " << s_n << " " << s_u
<< std::endl;
}
}
For more details on GESP exam syllabi, past exam questions, knowledge expansion, and practice lists, see:
【Pinned】【GESP】C++ Certification Learning Resource Summaryhttps://www.coderli.com/gesp-resource-summary/
“luogu- series problems can be evaluated online at Luogu Problem Bank.
“bcqm- series problems can be evaluated online at Programming Enlightenment Problem Bank.