PTA(A)1074 Reversing Linked List (25point(s))(坑)

PTA(A)1074 Reversing Linked List (25point(s))(坑)_第1张图片
思路:
最后一个点是坑,可能存在孤立点。
代码:

#include 
#include 
#include 
#include 

using namespace std;

struct node {
     
    int address, next;
    int data;
} mp[100005];
vector<node>a;

int main()
{
     
    int s;
    int n, k;
    cin >> s >> n >> k;

    for (int i = 0; i < n; ++i)
    {
     
        int sx, ex;
        int d;
        cin >> sx >> d >> ex;
        mp[sx].address = sx;
        mp[sx].data = d;
        mp[sx].next = ex;
    }

    int cnt = 0;

    for (int i = s; i != -1; i = mp[i].next) a.push_back(mp[i]);
    cnt = a.size();

    for (int i = 0; i < cnt - cnt % k; i += k)
        reverse(a.begin() + i, a.begin() + i + k);

    printf("%05d %d ", a[0].address, a[0].data);
    for (int i = 1; i < a.size(); ++i)
    {
     
        printf("%05d\n%05d %d ", a[i].address, a[i].address, a[i].data);
    }
    printf("-1");

    return 0;
}
// 日天,这道题最后一个点应该会有孤立点产生。。。

你可能感兴趣的:(PAT,(Advanced,Level),Practice)