给定一个常数K以及一个单链表L,请编写程序将L中每K个结点反转。例如:给定L为1→2→3→4→5→6,K为3,则输出应该为3→2→1→6→5→4;如果K为4,则输出应该为4→3→2→1→5→6,即最后不到K个元素不反转。
输入格式:
每个输入包含1个测试用例。每个测试用例第1行给出第1个结点的地址、结点总个数正整数N(<= 105)、以及正整数K(<=N),即要求反转的子链结点的个数。结点的地址是5位非负整数,NULL地址用-1表示。
接下来有N行,每行格式为:
Address Data Next
其中Address是结点地址,Data是该结点保存的整数数据,Next是下一结点的地址。
输出格式:
对每个测试用例,顺序输出反转后的链表,其上每个结点占一行,格式与输入相同。
输入样例:00100 6 4 00000 4 99999 00100 1 12309 68237 6 -1 33218 3 00000 99999 5 68237 12309 2 33218输出样例:
00000 4 33218 33218 3 12309 12309 2 00100 00100 1 99999 99999 5 68237 68237 6 -1
分析:
这道题目···到最后也没做出来。一是注意超时的问题····二是有些节点是没有用的····有一个测试点始终过不了,真是蛋疼~以后再说吧~
using System; using System.Collections.Generic; namespace PAT { class Program { public static Node[] nodes = new Node[100000]; static void Main(string[] args) { string[] infos = Console.ReadLine().Split(' '); int beginAddress = int.Parse(infos[0]); int length = int.Parse(infos[1]); int unit = int.Parse(infos[2]); int count = 0; //有些节点是废节点 GetNodes(length); List<Node> sortedNodes = new List<Node>(length); int currentAddress = beginAddress; if (!nodes[currentAddress].used) Console.Write("-1"); else { do { sortedNodes.Add(nodes[currentAddress]); currentAddress = nodes[currentAddress].next; count++; } while (currentAddress != -1); List<Node> areaNodes = new List<Node>(unit); int nextAddress; for (int i = 0; i < count; i++) { if (i != 0 && i % unit == 0) { nextAddress = sortedNodes[i].address; SortArea(areaNodes, unit, nextAddress); areaNodes = new List<Node>(unit); } areaNodes.Add(sortedNodes[i]); } if (areaNodes.Count == unit) SortArea(areaNodes, unit, -1); else if (areaNodes.Count != 0) { int index; for (index = 0; index < areaNodes.Count - 1; index++) { Console.WriteLine("{0:00000} {1:00000} {2:00000}", areaNodes[index].address, areaNodes[index].value, areaNodes[index + 1].address); } Console.WriteLine("{0:00000} {1:00000} {2}", areaNodes[index].address, areaNodes[index].value, "-1"); } } } static void GetNodes(int length) { string[] infos; int address; for (int i = 0; i < length; i++) { infos = Console.ReadLine().Split(' '); address = int.Parse(infos[0]); nodes[address].address = address; nodes[address].value = infos[1]; nodes[address].next = int.Parse(infos[2]); nodes[address].used = true; } } static void SortArea(List<Node> areaNodes, int areaLength, int nextAreaAddress) { int i; for (i = 0; i < areaLength - 1; i++) { Console.WriteLine("{0:00000} {1:00000} {2:00000}", areaNodes[areaLength - i - 1].address, areaNodes[areaLength - i - 1].value, areaNodes[areaLength - i - 2].address); } if (nextAreaAddress != -1) Console.WriteLine("{0:00000} {1:00000} {2:00000}", areaNodes[areaLength - i - 1].address, areaNodes[areaLength - i - 1].value, nextAreaAddress); else Console.WriteLine("{0:00000} {1:00000} {2}", areaNodes[areaLength - i - 1].address, areaNodes[areaLength - i - 1].value, nextAreaAddress); } public struct Node { public int address; public string value; public int next; public bool used; } } }
#include<iostream> #include<vector> #include<cstdio> using namespace std; struct node { int add; int data; int next; }; int main() { vector<node> vin(100000);//输入时暂存节点 vector<node> vsorted;//暂存排序后的结果 vector<node> vout;//最后的结果 node temp; int first,N,K; scanf("%d%d%d",&first,&N,&K); for(int i = 0;i<N;i++)//输入 { //cin>>temp.add>>temp.data>>temp.next; scanf("%d%d%d",&temp.add,&temp.data,&temp.next); vin[temp.add]=temp; } if(first == -1) //首地址为-1.直接输出 { printf("-1\n"); } else { int nextAdd = first; while(nextAdd !=-1) { vsorted.push_back(vin[nextAdd]); nextAdd = vin[nextAdd].next; } int Nnew = vsorted.size();//排序后的链的大小,不一定等于N,因为可能存在废点不在链上 int right = K-1; while(right < Nnew) { for(int i=right;i>right-K;i--) { vout.push_back(vsorted[i]); } right += K; } for(int i = right-K+1;i<Nnew ;i++) { vout.push_back(vsorted[i]); } for(int i =0;i<Nnew-1;i++) { vout[i].next = vout[i+1].add; printf("%05d %d %05d\n",vout[i].add,vout[i].data,vout[i].next); //cout<<setw(5)<<setfill('0')<<vout[i].add<<" "<<vout[i].data<<" "<<setw(5)<<setfill('0')<<vout[i].next<<endl; } printf("%05d %d %d\n",vout[Nnew-1].add,vout[Nnew-1].data,-1);//最后地址-1 陷阱之一 //cout<<setw(5)<<setfill('0')<<vout[Nnew-1].add<<" "<<vout[Nnew-1].data<<" "<<vout[Nnew-1].next<<endl; } system("pause"); return 0; }