生活随笔
收集整理的這篇文章主要介紹了
【PAT乙级】1025 反转链表 (25 分)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目地址
最后一個測試點:不是所有的點都是合法的,有的結點地址是負數。
#include<cstdio>
#include<iostream>
#include<map>
#include<stack>
using namespace std
;
const int N
=1e5+10;
struct node
{int id
;int val
;
}Node
[N
];
map
<int,int>mp
,v
;
int start
,n
,k
;
int main(void)
{cin
>>start
>>n
>>k
;for(int i
=0;i
<n
;i
++){int a
,b
,c
; cin
>>a
>>b
>>c
;mp
[a
]=c
,v
[a
]=b
;}int index
=0;while(1){Node
[index
].id
=start
,Node
[index
].val
=v
[start
];start
=mp
[start
];if(Node
[index
].id
>=0) index
++;if(start
==-1) break;}start
=0;while(1){if(start
+k
>index
) break;stack
<node
>st
;for(int i
=start
;i
<start
+k
;i
++) st
.push(Node
[i
]);for(int i
=start
;i
<start
+k
;i
++) Node
[i
]=st
.top(),st
.pop();start
=start
+k
;}for(int i
=0;i
<index
;i
++){printf("%05d %d ",Node
[i
].id
,Node
[i
].val
);if(i
+1==index
) printf("-1\n");else printf("%05d\n",Node
[i
+1].id
);}return 0;
}
#include<bits/stdc++.h>
using namespace std
;
const int N
=1e5+10;
struct node
{int id
,val
;
}temp
;
int start
,n
,k
;
map
<int,int>val
,nxt
;
vector
<node
>ans
;
int main(void)
{cin
>>start
>>n
>>k
;for(int i
=0;i
<n
;i
++){int a
,b
,c
; cin
>>a
>>b
>>c
;val
[a
]=b
,nxt
[a
]=c
;}while(start
!=-1){temp
.id
=start
,temp
.val
=val
[start
];ans
.push_back(temp
);start
=nxt
[start
];}int index
=0;while(1){if(index
+k
>ans
.size()) break;reverse(ans
.begin()+index
,ans
.begin()+index
+k
);index
+=k
;}for(int i
=0;i
<ans
.size();i
++){printf("%05d %d ",ans
[i
].id
,ans
[i
].val
);if(i
+1==ans
.size()) cout
<<-1;else printf("%05d\n",ans
[i
+1].id
);}return 0;
}
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀
總結
以上是生活随笔為你收集整理的【PAT乙级】1025 反转链表 (25 分)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。