首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

PAT乙级-1075 链表元素分类 (25分)

  • 24-02-22 07:00
  • 2781
  • 8414
blog.csdn.net

点击链接PAT乙级-AC全解汇总

题目:
给定一个单链表,请编写程序将链表元素进行分类排列,使得所有负值元素都排在非负值元素的前面,而 [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 个结点的地址;结点总个数,即正整数N (≤105​​ );以及正整数K (≤103​​ )。结点的地址是 5 位非负整数,NULL 地址用 −1 表示。

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

Address Data Next
  • 1

其中 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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

输出样例:

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

我的代码:

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
//有的时候题目是一起做的,所以会有不需要的头文件

int main()
{
    int Data[100010][2]={0};
    memset(Data,0,sizeof(Data));

    int add_first,num,K;
    cin>>add_first>>num>>K;
    for(int i=0;i<num;i++)
    {
        int add,data,next;
        cin>>add>>data>>next;
        Data[add][0]=data;
        Data[add][1]=next;
    }

    //假设A->B->C->D,ABD+,C-
    int last_negative=add_first;//最后一个负数
    int last_less_K=add_first;//最后一个小于K的数
    int index_t=add_first;//用来遍历的下标
    bool flag_have_negative=false;
    bool flag_have_less_K=false;
    while(1)
    {
        int index_t_next=Data[index_t][1];
        if(index_t_next==-1)break;
        if(Data[index_t_next][0]<0)
        {
            if(Data[index_t][0]>0)//往前放
            {
                Data[index_t][1]=Data[index_t_next][1];//第一步:摘出
                if(Data[add_first][0]<0)//last_negative->C
                {
                    Data[index_t_next][1]=Data[last_negative][1];//C插入last之后
                    Data[last_negative][1]=index_t_next;//C插入last之后
                }
                else//新头:C->last_negative
                {
                    Data[index_t_next][1]=add_first;//C插入到头部
                    add_first=index_t_next;//C为新头部
                }
            }
            else //就放在index_t后面不变
            {
                index_t=index_t_next;
            }
            //第三步:新的最后一个负数肯定是C
            last_negative=index_t_next;
            flag_have_negative=true;
            //如果没有第二类,下标得跟着往后移
            if(!flag_have_less_K)last_less_K=Data[last_negative][1];
        }
        else if(Data[index_t_next][0]>=0&&Data[index_t_next][0]<=K)
        {
            if(Data[index_t][0]>K)//往前放
            {
                Data[index_t][1]=Data[index_t_next][1];
                if(Data[last_less_K][0]>K)//last_less_K还是第一个数,放到last_less_K前面
                {
                    Data[index_t_next][1]=last_less_K;
                    add_first=index_t_next;
                }
                else//放到last_less_K后面
                {
                    Data[index_t_next][1]=Data[last_less_K][1];
                    Data[last_less_K][1]=index_t_next;
                }
            }
            else //就放在index_t后面不变
            {
                index_t=index_t_next;
            }
            last_less_K=index_t_next;
            flag_have_less_K=true;
        }
        else
        {
            index_t=Data[index_t][1];
        }
    }

    int index=add_first;
    while(Data[index][1]!=-1)
    {
        printf("%05d %d %05d\n",index,Data[index][0],Data[index][1]);
        index=Data[index][1];
    }
    printf("%05d %d -1",index,Data[index][0]);
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103

我用的几个测试案例:

1 2 5
1 -2 2
2 -3 -1
  • 1
  • 2
  • 3
1 3 5
1 5 2
2 -1 3
3 2 -1
  • 1
  • 2
  • 3
  • 4
1 6 5
1 -1 2
2 2 3
3 8 4
4 -2 5
5 4 6
6 9 -1
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
1 6 5
1 1 2
2 -2 3
3 8 4
4 2 5
5 -4 6
6 9 -1
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

这道题心态崩了
我怎么没想到用几个数组分别寸?或者多次遍历?
能够安慰自己的也就时间复杂度和空间复杂度都是O(n)了

注:本文转载自blog.csdn.net的邂逅模拟卷的文章"https://blog.csdn.net/qq_34451909/article/details/104965296"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2492) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

101
推荐
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2025 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top