1025

問題嚴重!

#include<iostream>
#include<string>
#include<vector>
#include<memory>


using namespace std;

class list_creat
{
public:
    list_creat(istream &in) { in >> address >> data >> next; }
    string& get_address() { return address; }
    long long& get_data() { return data; }
    string& get_next() { return next; }

private:
    long long data = 0;
    string address;
    string next;
};

class list_result
{
public:
    list_result(vector<list_creat> &l) :list_info(l) {};
    void sort(string address, unsigned n, unsigned k) { list_default(address); sort_by_format(n, k); };
    friend ostream &operator<<(ostream &os,const list_result &rhs);

private:
    vector<list_creat> list_info;
    void sort_by_format(unsigned n,unsigned k);
    void list_default(string cur_add);
};

void list_result::list_default(string cur_add)
{
    vector<list_creat> tmp;
    while (true)
    {
        for (auto &r : list_info)
        {
            if (cur_add == r.get_address())
            {
                tmp.push_back(r);
                cur_add = r.get_next();
                break;
            }
        }
        if (cur_add == "-1")
            break;
    }
    *this = tmp;
}
void list_result::sort_by_format(unsigned n,unsigned k)
{
    unsigned count = n / k;
    for (unsigned i = 0; i < count; ++i)
    {
        for (unsigned j = 0; j < k/2; ++j)
        {
            auto tmp = list_info[j + i*k];
            list_info[j + i*k] = list_info[i*k + k - j - 1];
            list_info[i*k + k - j - 1] = tmp;
        }
    }
    for (unsigned i=0;i<n-1;i++)
    {
        list_info[i].get_next() = list_info[i + 1].get_address();
    }
}

ostream &operator<<(ostream &os,const list_result &rhs)
{
    unsigned n = rhs.list_info.size();
    for (unsigned i = 0; i < n; ++i)
    {
        list_creat tmp = rhs.list_info.at(i);
        if (i != n - 1)
        {
            os << tmp.get_address() << " " << tmp.get_data() << " " << tmp.get_next() << endl;
        }
        else
        {
            os << tmp.get_address() << " " << tmp.get_data() << " " << tmp.get_next();
        }
    }
    return os;
}

int main()
{
    string first_add;
    unsigned n = 0;
    unsigned k = 0;
    cin >> first_add >> n >> k;

    vector<list_creat> in_list;
    for (unsigned i = 0; i < n; ++i)
    {
        in_list.push_back(list_creat(cin));
    }
    
    list_result result(in_list);
    result.sort(first_add, n, k);
    cout << result;
    
    cout << endl;
    system("pause");
    return 0;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容