Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

C++风格代码

Posted by xziyue at 2017-02-04 16:23:40 on Problem 1008
“冗余”,“低效”

#include <string>
#include <vector>
#include <iostream>
#include <sstream>
#include <stdlib.h>

using namespace std;

string raw_haab_months(
        "pop, no, zip, zotz, tzec, xul, yoxkin, mol, chen, yax, zac, ceh, mac, kankin, muan, pax, koyab, cumhu, uayet");
string raw_tzolkin_days(
        " imix, ik, akbal, kan, chicchan, cimi, manik, lamat, muluk, ok, chuen, eb, ben, ix, mem, cib, caban, eznab, canac, ahau");

vector<string> *generate_set_from_string(string &str) {
    size_t pos;
    while ((pos = str.find(' ')) != string::npos) {
        str.erase(pos, 1);
    }
    vector<string> *ret = new vector<string>;
    ret->reserve(20);

    while ((pos = str.find(',')) != string::npos) {
        ret->push_back(str.substr(0, pos));
        str.erase(0, pos + 1);
    }

    ret->push_back(str);
    return ret;
}

void assert(bool condition) {
    if (!condition)throw exception();
}

class calender {

    virtual vector<string> *names() = 0;

    friend int convert_from_str(string _str, calender &c) {
        for (int i = 0; i < c.names()->size(); i++) {
            if (_str == c.names()->at(i)) {
                return i;
            }
        }
        throw exception();
    }

public:

    virtual int to_universal_day() = 0;

    virtual void from_universal_day(int uni_day) = 0;

    virtual string to_string() = 0;

    static const int undecided;
};

const int calender::undecided = -1;

class haab : public calender {
private:

    static vector<string> *vec;

    virtual vector<string> *names() {
        return vec;
    }

public:
    int day, month, year;

    haab() {
        day = 0;
        month = 0;
        year = 0;
    }

    haab(int uni_day) {
        from_universal_day(uni_day);
    }

    haab(int _day, const string &_month, int _year) {
        day = _day;
        month = convert_from_str(_month, *this);
        year = _year;
    }

    virtual int to_universal_day() {
        int ret = 365 * year + 20 * month + (day + 1);
        return ret;
    }

    virtual void from_universal_day(int uni_day) {
        year = uni_day / 365;
        if (year > 0 && uni_day % 365 == 0) {
            year -= 1;
        }
        month = (uni_day - 365 * year) / 20;
        day = uni_day - 365 * year - 20 * month;
    }

    virtual string to_string() {
        stringstream sstr;
        sstr << day << ". " << names()->at(month) << " " << year << endl;
        return sstr.str();
    }
};

vector<string> *haab::vec = generate_set_from_string(raw_haab_months);

class tzolkin : public calender {
private:
    static vector<string> *vec;

    virtual vector<string> *names() {
        return vec;
    }

public:
    int index, name, year;

    tzolkin() {
        index = 1;
        name = 0;
        year = 0;
    }

    tzolkin(int uni_day) {
        from_universal_day(uni_day);
    }

    tzolkin(int _index, const string &_name, int _year) {
        index = _index;
        name = convert_from_str(_name, *this);
        year = _year;
    }

    virtual int to_universal_day() {
        return calender::undecided;
    }

    virtual void from_universal_day(int uni_day) {
        year = uni_day / 260;
        if (year > 0 && uni_day % 260 == 0) {
            year -= 1;
        }
        name = (uni_day - 1) % 20;
        index = ((uni_day - 1) % 13) + 1;
    }

    virtual string to_string() {
        stringstream sstr;
        sstr << index << " " << names()->at(name) << " " << year << endl;
        return sstr.str();
    }

};

vector<string> *tzolkin::vec = generate_set_from_string(raw_tzolkin_days);

void convert(calender &from, calender &to) {
    int uni_day = from.to_universal_day();
    to.from_universal_day(uni_day);
}

int main() {
    int count;
    cin >> count;

    vector<calender *> data;
    data.reserve(count);

    string raw, month;
    int day, year;
    for (int i = 0; i < count; i++) {
        raw.clear();
        month.clear();
        cin >> raw >> month >> year;
        day = atoi(raw.substr(0, raw.length() - 1).c_str());
        haab *htemp = new haab(day, month, year);
        data.push_back(static_cast<calender *>(htemp));
    }

    cout << data.size() << endl;
    for (int i = 0; i < count; i++) {
        tzolkin temp;
        convert(*(data[i]), temp);
        cout << temp.to_string();
    }
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator