ZJANS

b982. YoJudge 預練(空間之章)

Easy Last Update: 2026/01/29
字串

將各種格式的時間單位統一轉換為毫秒
可能出現的格式如下:

格式代表
xhourxx 小時
xhymxx 小時又 yy 分鐘
xhymzsxx 小時又 yy 分鐘又 zz
yminyy 分鐘
ymzsyy 分鐘又 zz
zszz
z.aszz 秒又 a×100a \times 100 毫秒
bmsbb 毫秒

解法一

✅ 完整代碼

評分結果(參考) : AC (2ms, 336KB)

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

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    string s;
    while(cin >> s){
        int ans=0, num=0;
        for(int i=0; i<s.size(); i++){
            if(s[i]>='0' && s[i]<='9'){
                num = num*10 + (s[i]-'0');
            }
            else if(s.substr(i, 2) == "gb"){
                ans += num*1e9*8;
                i += 1;
                num = 0;
            }
            else if(s.substr(i, 2) == "mb"){
                ans += num*1e6*8;
                i += 1;
                num = 0;
            }
            else if(s.substr(i, 2) == "kb"){
                ans += num*1e3*8;
                i += 1;
                num = 0;
            }
            else if(s.substr(i, 4) == "byte"){
                ans += num*8;
                i += 3;
                num = 0;
            }
            else if(s.substr(i, 3) == "bit"){
                ans += num;
                i += 2;
                num = 0;
            }
            else if(s[i] == 'g'){
                ans += num*1e9*8;
                num = 0;
            }
            else if(s[i] == 'm'){
                ans += num*1e6*8;
                num = 0;
            }
            else if(s[i] == 'k'){
                ans += num*1e3*8;
                num = 0;
            }
            else if(s[i] == '.' && s.substr(i+2, 2) == "kb"){
                ans += num*1e3*8 + (s[i+1]-'0')*100*8;
                break;
            }
            else if(s[i] == '.' && s.substr(i+2, 4) == "byte"){
                ans += num*8 + (s[i+1]-'0');
                break;
            }
        }
        cout << ans << "\n";
    }
    
    return 0;
}