1075. 链表元素分类(25)

题目链接:https://www.patest.cn/contests/pat-b-practise/1075

给定一个单链表,请编写程序将链表元素进行分类排列,使得所有负值元素都排在非负值元素的前面,而[0, K]区间内的元素都排在大于K的元素前面。但每一类内部元素的顺序是不能改变的。例如:给定链表为 18→7→-4→0→5→-6→10→11→-2,K为10,则输出应该为 -4→-6→-2→7→0→5→10→18→11。

输入格式:

每个输入包含1个测试用例。每个测试用例第1行给出:第1个结点的地址;结点总个数,即正整数N (<= 105);以及正整数K (<=1000)。结点的地址是5位非负整数,NULL地址用-1表示。


接下来有N行,每行格式为:


Address Data Next


其中Address是结点地址;Data是该结点保存的数据,为[-105, 105]区间内的整数;Next是下一结点的地址。题目保证给出的链表不为空。


输出格式:



对每个测试用例,按链表从头到尾的顺序输出重排后的结果链表,其上每个结点占一行,格式与输入相同。


输入样例:
00100 9 10
23333 10 27777
00000 0 99999
00100 18 12309
68237 -6 23333
33218 -4 00000
48652 -2 -1
99999 5 68237
27777 11 48652
12309 7 33218
输出样例:
33218 -4 68237
68237 -6 48652
48652 -2 12309
12309 7 00000
00000 0 99999
99999 5 23333
23333 10 00100
00100 18 27777
27777 11 -1


下面的代码能通过。

#include <iostream>
#include <vector>
#include <cstdio>
using namespace std;
typedef struct node{
	int address;
	int data;
	int next;
	node(){}
	node(int address, int data, int next){
		this->address = address;
		this->data = data;
		this->next = next;
	}
} Node;
int main(int argc, char** argv) {
	int head; int n, k;
	cin >> head >> n >> k;
	vector<int> seq;//存新顺序的关键字
	vector<Node> vec(100005, Node());
	int t, ne; int d, i;
	for (i = 0; i < n; i++) {
		cin >> t >> d >> ne;
		vec[t] = Node(t, d, ne);
	}
	t = head;
	while (t != -1){
		if (vec[t].data <0){
			seq.push_back(vec[t].address);
		}
		t = vec[t].next;
	}
	t = head;
	while (t != -1){
		if (vec[t].data >= 0 && vec[t].data <= k){
			seq.push_back(vec[t].address);
		}
		t = vec[t].next;
	}
	t = head;
	while (t != -1){
		if (vec[t].data >  k){
			seq.push_back(vec[t].address);
		}
		t = vec[t].next;
	}
	int len = seq.size();
	for (i = 0; i < len; i++){//len用变量n来代替,就有一个测试点通不过,好塞
		if (i != len - 1)
			printf("%05d %d %05d\n", seq[i], vec[seq[i]].data, seq[i + 1]);
		else
			printf("%05d %d -1\n", seq[i], vec[seq[i]].data);
	}
	return 0;
}

但上面代码中的n表示结点数,用它代替代码中的len就有一项通不过,这是为什么?好久也解决不了。
另外,方法上用变量统计三段数据的个数,用一个数组或vector变量保存新次序的结点地址,但还是那个测试点通不过。代码如下。

#include <iostream>
#include <vector>
#include <cstdio>
#include <limits.h>//为使用INT_MAX 
using namespace std;
typedef struct node{
	int address;
	int dat;
	int next;
	node(){address=INT_MIN;dat=INT_MIN;next=-1;}
	node(int address, int data, int next){
		this->address = address;
		this->dat = data;
		this->next = next;
	}
} Node;
int head; int n, k;
int main(int argc, char** argv) {
	//freopen("e:\\input.txt", "r", stdin);
	cin >> head >> n >> k;
	vector<int> seq(n+8);//存新顺序的关键字
	vector<Node> vec(100009);
	int t, ne; int d, i, n1 = 0, n2 = 0;//表示负数个数,[0,K]个数
	for (i = 0; i < n; i++) {
		cin >> t >> d >> ne;
		vec[t]=Node(t, d, ne);
		if (d < 0){
			n1++;
		}else if (d <= k && d>=0) n2++;
	}
	//fclose(stdin);
	int pos1 = 0, pos2 = n1, pos3 =n1+n2;//分别表示seq中三段的起始位置 
	t = head;
	while (t != -1){
		if(vec[t].dat<0) seq[pos1++]=vec[t].address;
		else if(vec[t].dat>k) seq[pos3++]=vec[t].address;
		else seq[pos2++]=vec[t].address;
		t=vec[t].next;
	}
	for (i = 0; i < n;i++){
		if (i!=n-1)
			printf("%05d %d %05d\n", seq[i], vec[seq[i]].dat, seq[i+1]);
		else
			printf("%05d %d -1\n", seq[i], vec[seq[i]].dat);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
这道题目要求我们将一个链表中的元素按照奇偶性分成两个链表,其中奇数链表中的元素顺序与原链表中的顺序相同,偶数链表中的元素顺序与原链表中的顺序相反。 我们可以使用两个指针分别指向奇数链表和偶数链表的尾部,然后遍历原链表,将奇数节点插入奇数链表的尾部,偶数节点插入偶数链表的头部。最后将偶数链表反转即可。 具体实现可以参考以下代码: ```cpp #include <iostream> #include <vector> using namespace std; struct ListNode { int val; ListNode* next; ListNode(int x) : val(x), next(NULL) {} }; ListNode* createList(vector<int>& nums) { ListNode* head = new ListNode(0); ListNode* p = head; for (int num : nums) { p->next = new ListNode(num); p = p->next; } return head->next; } void printList(ListNode* head) { while (head) { cout << head->val << " "; head = head->next; } cout << endl; } ListNode* oddEvenList(ListNode* head) { if (!head || !head->next) return head; ListNode* oddTail = head; ListNode* evenHead = head->next; ListNode* evenTail = evenHead; while (evenTail && evenTail->next) { oddTail->next = evenTail->next; oddTail = oddTail->next; evenTail->next = oddTail->next; evenTail = evenTail->next; } oddTail->next = evenHead; ListNode* p = evenHead; ListNode* q = NULL; while (p) { ListNode* tmp = p->next; p->next = q; q = p; p = tmp; } evenHead = q; oddTail->next = evenHead; return head; } int main() { vector<int> nums = {1, 2, 3, 4, 5}; ListNode* head = createList(nums); printList(head); head = oddEvenList(head); printList(head); return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值