GESP C++ Level 3 Programming (54C++): Unit Conversion – The Versatile Enumeration

For the GESP Level 2 Exam: C++ 202312 Level 3 Unit ConversionThis problem is relatively simple, but there is a major difficulty: it is hard to understand, with nearly 700 words in total, reading the question is quite exhausting, it feels like a reading comprehension exercise.Look at the question:GESP C++ Level 3 Programming (54C++): Unit Conversion - The Versatile EnumerationGESP C++ Level 3 Programming (54C++): Unit Conversion - The Versatile Enumeration

Problem-solving approach:

First: we can understand the conversion from kg to g or mg. Additionally, the input unit must be greater than the output unit. This means we only perform multiplication. Either *1000; or *1000*1000;

Secondly: For a conversion involving three units, there are only three possible cases.

Finally: For a conversion between two units, there are only 6 possible cases. Direct enumeration can accomplish this.


#include <bits/stdc++.h>
using namespace std;
int main(){
    int n, m, b;
    string dw, mbdw, dy, wenh;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        cin >> m >> dw >> dy >> wenh >> mbdw;
        cout << m << " " << dw << " " << dy << " ";
        if (dw == "m" && mbdw == "mm")
        {
            b = m * 1000;
        }
        if (dw == "km" && mbdw == "m")
        {
            b = m * 1000;
        }
        if (dw == "km" && mbdw == "mm")
        {
            b = m * 1000 * 1000;
        }
        if (dw == "g" && mbdw == "mg")
        {
            b = m * 1000;
        }
        if (dw == "kg" && mbdw == "g")
        {
            b = m * 1000;
        }
        if (dw == "kg" && mbdw == "mg")
        {
            b = m * 1000 * 1000;
        }
        cout << b << " " << mbdw << endl;
    }
    return 0;
}

Isn’t it very simple?

Programming! The focus is on training thought processes, completing the most complex tasks with the simplest thinking. Of course, a simple thought process does not mean the code is concise.In everything, first complete, then perfect!Therefore, this code has many areas for optimization…

Have you learned it?GESP C++ Level 3 Programming (54C++): Unit Conversion - The Versatile Enumeration

Leave a Comment