pat解题报告【1074】

1074. Reversing Linked List (25)

时间限制  
300 ms
内存限制  
32000 kB
代码长度限制  
16000 B
判题程序    
Standard    
作者    
CHEN, Yue

Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements on L.  For example, given L being 1→2→3→4→5→6, if K = 3, then you must output 3→2→1→6→5→4; if K = 4, you must output 4→3→2→1→5→6.

Input Specification:

Each input file contains one test case.  For each case, the first line contains the address of the first node, a positive N (<= 105) which is the total number of nodes, and a positive K (<=N) which is the length of the sublist to be reversed.  The address of a node is a 5-digit nonnegative integer, and NULL is represented by -1.

Then N lines follow, each describes a node in the format:

Address Data Next

where Address is the position of the node, Data is an integer, andNext is the position of the next node.

Output Specification:

For each case, output the resulting ordered linked list.  Each node occupies a line, and is printed in the same format as in the input.

Sample Input:
00100 6 4
00000 4 99999
00100 1 12309
68237 6 -1
33218 3 00000
99999 5 68237
12309 2 33218
Sample Output:
00000 4 33218
33218 3 12309
12309 2 00100
00100 1 99999
99999 5 68237
68237 6 -1

反转链表,但是此链表非彼链表。题目意思是说链表的节点由两个地址一个数值组成,给定一个数k,那么链表里面以k个节点为一组进行反转,但是要保证各节点之间的地址相连无误。

刚开始的思路是:用字符串来存储地址,因为地址一定是5位数,结果最后倒数第二个点会超时。看来读入一个类真的很费时间,后来改成了char[ ]虽然超时没了,但是判断起来还是很不方便。后来看了别人的代码发现这么个东西:printf("%05d",n);用5位长度输出一个数字,少的部分用0来填充。这样就可以用int来存地址,映射完全可以用数组来替代。

除了上述数据结构的简化,这个题目还有几点要注意:

【1】输入的点可能有无用的点,这些点要忽略。

【2】输出-1的时候前面没有0

【3】反转的时候节点本身的地址是不变的,但是下一个地址要变,这里我用了输出两次本次地址来替代。

【4】注意反转两轮及以上的情况。

Ac代码:

// pat-1074-ans.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include"iostream"
#include"stdio.h"
#include "deque"
using namespace std;
class node{
public:
	int addr;
	int val;
	int next;
	node(int a,int v,int n):addr(a),val(v),next(n){}
	node(){}
};
node an[100000];
deque ans;
int time=0;
void output(deque& ans)
{  
	if (time>0&&!ans.empty())
	{
		printf("%05d\n",ans.back().addr);
		time--;
	}
	int index=ans.size()-2;
	int temp=ans.back().next;
	while (!ans.empty())
	{   
		if (index==-1)//最后一个
		{
			printf("%05d",ans.back().addr);
			printf(" %d ",ans.back().val);
			
		    time++;
		}
		else
		{
			printf("%05d",ans.back().addr);
			printf(" %d ",ans.back().val);
			if (ans.at(index).addr==-1)
			{
				printf("%d\n",ans.at(index).addr);
			}else
				printf("%05d\n",ans.at(index).addr);
		}
	
		ans.pop_back();
		index-=1;
	}
}
void output_front(deque& ans)
{

	if (time>0&&!ans.empty())
	{
		printf("%05d\n",ans.front().addr);
		time--;
	}
	while (!ans.empty())
	{   
		printf("%05d",ans.front().addr);
		printf(" %d ",ans.front().val); 
		if (ans.back().next==-1)
		{
			printf("%d\n",ans.front().next);
		}else
			printf("%05d\n",ans.front().next);
		ans.pop_front();
	}
}
int main()
{  
	int n=0,k=0,head=0;
	cin>>head>>n>>k;
	int addr=0,val=0,next=0;
	for (int i=0;i>addr>>val>>next;
		an[addr].addr=addr;
		an[addr].val=val;
		an[addr].next=next;
	}
	int cnt=0;
	int index=head;
	bool apend=false;
	while(cnt


 

 

你可能感兴趣的:(c++,数据结构,算法)