【練習】PTA B1025 反轉鏈表

有一個測試用例無法通過。

https://pintia.cn/problem-sets/994805260223102976/problems/994805296180871168

#include <cstdio>
#include <cstdlib>
#include <algorithm>
using namespace std;
const int maxn=100010;
struct Node{
	int address,data,next;
	bool flag;
}node[maxn];
int main(){
	int begin,n,k;
	int arrLoc[maxn];
	scanf("%d %d %d",&begin,&n,&k);
	for(int i=0;i<n;i++){
		int address,data,next;
		scanf("%d %d %d",&address,&data,&next);
		node[address].data=data;
		node[address].next=next;
		node[address].flag=false;
	}
	int p=begin,count=0,j=0,m;
	while(p!=-1){
		count++;
		node[p].flag=true;
		arrLoc[j++]=p;
		p=node[p].next;
	}
	m=n/k;
	for(int i=0;i<m;i++){
		reverse(arrLoc+(i*k),arrLoc+(i*k+k));
	}
	for(int i=0;i<count-1;i++){
		printf("%05d %d %05d\n",arrLoc[i],node[arrLoc[i]].data,arrLoc[i+1]);
	}
	printf("%05d %d -1\n",arrLoc[count-1],node[arrLoc[count-1]].data);
	system("pause");
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章