【PAT】2-1 Reversing Linked List

题目地址:2-1

按给定的K个间隔翻转链表。给出了链表的首地址和结点个数以及间隔K,每个结点又提供了自身的地址、存储的数值以及下一个结点的地址。结点构造成一个结构体,所有结点放在结构体数组里,其中注意存储的技巧——将地址作为数组的数值下标,而数组值是数据内容以及下一个节点的地址。同时注意存在无效的结点。

手残感觉输出的时候好麻烦。

#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <ctime>
using namespace std;

#define read() freopen("in.txt", "r", stdin)
#define write() freopen("out.txt", "w", stdout)
#define rep( i , a , b ) for ( int i = ( a ) ; i <  ( b ) ; ++ i )
#define For( i , a , b ) for ( int i = ( a ) ; i <= ( b ) ; ++ i )
#define clr( a , x ) memset ( a , x , sizeof a )
#define cpy( a , x ) memcpy ( a , x , sizeof a )
#define max(a,b) (a>b)?(a):(b)
#define LL long long
#define MaxSize 100004

typedef struct node
{
	int addr;
	int data;
	int next;
}Node;
Node nod[MaxSize];
Node rev[MaxSize];
int main()
{
	read();
	int start,n,k,rest,gap,i,j;
	while(scanf("%d %d %d",&start,&n,&k)!=EOF)
	{
		//按照题目要求输入每个结点的地址,数据以及下一个结点的地址
		//将地址作为数组的数值下标,而数组值是数据内容以及下一个节点地址
		int i = 0, j = 0;
		while(n--)
		{
			scanf("%d",&i);
			nod[i].addr = i;
			int a,b;
			scanf("%d",&a);
			nod[i].data = a;
			scanf("%d",&b);
			nod[i].next = b;
		}
		//构造单链表
	    for (int i = start;;)
	    {
		    rev[j].addr = nod[i].addr;
		    rev[j].data = nod[i].data;
		    j++;
		    i = nod[i].next;
		    if (i == -1)
		    {
			    break;
		    }
	    }

        n = j;//更新n,处理无效结点
	    //每k个结点逆置
	    gap = n / k;
	    rest = n % k;
	    for (i = 0; i < gap; ++i)
	    {
	    	for (j = (i + 1)*k - 1; j > i*k ; --j)
	    	{
	    		printf("%05d %d %05d\n",rev[j].addr, rev[j].data,rev[j-1].addr);
	    	}
	    	printf("%05d %d ",rev[j].addr,rev[j].data );
	    	if (rest == 0)
	    	{
	    		if (i == gap - 1)
	    		{
	    			printf("-1");
	    		}else
	    		{
	    			printf("%05d",rev[(i+2)*k-1].addr );
	    		}
	    	}else
	    	{
	    		if (i == gap - 1)
	    		{
	    			printf("%05d",rev[(i+1)*k].addr );
	    		}else
	    		{
	    			printf("%05d",rev[(i+2)*k-1].addr );

	    		}
	    	}
	    	printf("\n");
	    }

	    if (rest != 0)
	    {
	    	for (i = gap*k; i < n - 1; ++i)
	    	{
	    		printf("%05d %d %05d\n",rev[i].addr,rev[i].data,rev[i+1].addr );
	    	}
	    	printf("%05d %d -1\n",rev[i].addr,rev[i].data );
	    }
	}
    return 0;

}

  

  

时间: 2024-08-28 04:11:06

【PAT】2-1 Reversing Linked List的相关文章

【PAT】Emergency(最短路条数-SPFA)

[PAT]Emergency(最短路条数-SPFA) As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the length of each road betw

【leetcode】Intersection of Two Linked Lists(easy)

Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists: A: a1 → a2 c1 → c2 → c3 B: b1 → b2 → b3 begin to intersect at node c1. Notes: If the two linked lists have no i

【leetcode】Intersection of Two Linked Lists

Intersection of Two Linked Lists Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists: A: a1 → a2 c1 → c2 → c3 B: b1 → b2 → b3 begin to intersect at node c1. Notes:

【Leetcode】Intersection of Two Linked Lists in JAVA

Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists: A: a1 → a2 c1 → c2 → c3 B: b1 → b2 → b3 begin to intersect at node c1. 还是倒着想,把listnode放进栈里,然后一个一个pop出来,直到不一样的话,

LeetCode【160】Intersection of Two Linked Lists

For example, the following two linked lists: A: a1 → a2 c1 → c2 → c3 B: b1 → b2 → b3 begin to intersect at node c1. 思路比较清晰,首先确定二者的长度并计算长度之差的绝对值dis,让较长链表先走dis步,然后两个指针一起走.如果两个指针相等,则返回,否则当指针达到NULL时,没有交点. AC代码如下: void getLength(ListNode *head , int& len)

【Leetcode】 328. Odd Even Linked List

Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes. You should try to do it in place. The program should run in O(1) space complexi

【PAT】我要通过!

"答案正确"是自动判题系统给出的最令人欢喜的回复.本题属于PAT的"答案正确"大派送 -- 只要读入的字符串满足下列条件,系统就输出"答案正确",否则输出"答案错误". 得到"答案正确"的条件是: 1. 字符串中必须仅有P, A, T这三种字符,不可以包含其它字符:2. 任意形如 xPATx 的字符串都可以获得"答案正确",其中 x 或者是空字符串,或者是仅由字母 A 组成的字符串:3.

【PAT】1002. 写出这个数 (20)

1002. 写出这个数 (20) 读入一个自然数n,计算其各位数字之和,用汉语拼音写出和的每一位数字. 输入格式:每个测试输入包含1个测试用例,即给出自然数n的值.这里保证n小于10100. 输出格式:在一行内输出n的各位数字之和的每一位,拼音数字间有1 空格,但一行中最后一个拼音数字后没有空格. 输入样例: 1234567890987654321123456789 输出样例: yi san wu C 代码如下: 1 #include <stdio.h> 2 #include <stdl

【PAT】1004. 成绩排名 (20)

1004. 成绩排名 (20) 读入n名学生的姓名.学号.成绩,分别输出成绩最高和成绩最低学生的姓名和学号. 输入格式:每个测试输入包含1个测试用例,格式为 第1行:正整数n 第2行:第1个学生的姓名 学号 成绩 第3行:第2个学生的姓名 学号 成绩 ... ... ... 第n+1行:第n个学生的姓名 学号 成绩 其中姓名和学号均为不超过10个字符的字符串,成绩为0到100之间的一个整数,这里保证在一组测试用例中没有两个学生的成绩是相同的. 输出格式:对每个测试用例输出2行,第1行是成绩最高学