【題解】Codeforces 600A. Extract Numbers

【題目敘述】https://codeforces.com/problemset/problem/600/A

#include <iostream>
using namespace std;

int main() {
    string s, a = "\"", b = "\"";
    cin >> s;
    bool isNum = true;
    string temp = "";
    for (int i = 0; i < s.size(); i++) {
        if (s[i] == ',' || s[i] == ';') {
            if (temp.size() && isNum && (temp[0] != '0' || (temp.size() == 1))) {
                a += temp;
                a += ",";
            } else {
                b += temp;
                b += ",";
            }
            temp = "";
            isNum = true;
        } else {
            temp += s[i];
            if (s[i] == '.') {
                isNum = false;
            }
            if (!(s[i] >= '0' && s[i] <= '9')) {
                isNum = false;
            }
        }
    }
    if (temp.size()){
        if (isNum && (temp[0] != '0' || (temp.size() == 1))) {
            a += temp;
            a += ",";
        } else {
            b += temp;
            b += ",";
        }
    }
    if (s[s.size()-1] == ',' || s[s.size()-1] == ';') b += ",";
    
    if (a[a.size()-1] == ',') a[a.size()-1] = '\"';
    else a = "-";
    
    if (b[b.size()-1] == ',') b[b.size()-1] = '\"';
    else b = "-";
    
    cout << a << "\n";
    cout << b << "\n";
    return 0;
}
分享本文 Share with friends