B3926 [GSEP202312 三级] 单位转换 题解

· · 题解

思路

我们可以按照单位的进率进行分类讨论:

由于题目说了“只涉及将更大的单位转换为更小的单位”,所以这样做是可行的。

还需要注意一下输入:输入是数字夹在字符串里,因为每部分有空格,所以我选择了用 cin 读入(因为 cin 读入会过滤掉空格,就会将接下来的值放到下一个变量当中了)。

代码如下:

#include <bits/stdc++.h>
using namespace std;

int t, n;
string dw1, dw2;
char tmp;

int main(){
    cin >> t;
    while(t --){
        cin >> n >> dw1 >> tmp >> tmp >> dw2;
        if((dw1 == "km" && dw2 == "m") || 
            (dw1 == "kg" && dw2 == "g") || 
            (dw1 == "m" && dw2 == "mm") ||
            (dw1 == "g" && dw2 == "mg"))
                cout << n << " " << dw1 << " " << '=' << " " << n * 1000 << " " << dw2 << endl;
        else
            cout << n << " " << dw1 << " " << '=' << " " << n * 1000000  << " " << dw2 << endl;
    }
    return 0;
}