zoukankan      html  css  js  c++  java
  • PAT A1052 Linked List Sorting (25分)


    给一个链表,然后按链表里面的数据排序,重新排成一个链表。

    注意给的数据里面有掺杂的无用节点,不能直接排序。
    所以应该遍历一遍链表之后标记出有用节点再排序。

    #include<cstdio>
    #include<map>
    #include <algorithm>
    using namespace std;
    const int N = 100010;
    struct Node{
        int address;
        int v;
        int next;
        bool flag = false;
    }node[N];
    
    bool cmp(Node a,Node b){
        if(a.flag==false)
            return false;
        else if(b.flag==false)
            return true;
        else{
            return a.v<b.v;
        }
    }
    int main(){
        int n,firstadd;
        scanf("%d %d",&n,&firstadd);
        int tempadd;
        for(int i = 0;i < n;i++){
            scanf("%d",&tempadd);
            scanf("%d %d",&node[tempadd].v,&node[tempadd].next);
            node[tempadd].address = tempadd;
            
        }
        int count=0;
        tempadd = firstadd;
        while(tempadd!=-1){
            node[tempadd].flag = true;
            tempadd = node[tempadd].next;
            count++;
        }   
        if(count==0){
            printf("0 -1");
        }else{
            //从小往大排序
            sort(node,node+N,cmp);
            firstadd = node[0].address;
            printf("%d %05d
    ",count,firstadd);
            for(int i = 0;i<count;i++){
                printf("%05d %d",node[i].address,node[i].v);
                if(i != count-1){            
                    printf(" %05d
    ",node[i+1].address);
                }else{
                    printf(" -1
    ");
                }            
            }
    
        }
        return 0;
                
    }
    
    

    代码二:map映射地址

    #include<cstdio>
    #include<map>
    using namespace std;
    const int N = 100010;
    map<int,int> mp;
    struct Node{
        int address;
        int v;
        int next;
    }node[N];
    
    int main(){
        int n,firstadd;
        scanf("%d %d",&n,&firstadd);
        int tempadd;
        for(int i = 0;i < n;i++){
            scanf("%d",&tempadd);
            scanf("%d %d",&node[tempadd].v,&node[tempadd].next);
            node[tempadd].address = tempadd;
            
        }
        tempadd = firstadd;
        while(tempadd!=-1){//遍历
            mp.insert(make_pair(node[tempadd].v,node[tempadd].address));
            tempadd = node[tempadd].next;
        }
        
        //mp从小往大排序
        if(mp.size()==0){
            printf("0 -1");
        }else{    
            int count = 0;
            map<int,int>::iterator it = mp.begin();
            firstadd = it->second;
            
            printf("%d %05d
    ",mp.size(),firstadd);
            while(it!=mp.end()){
                printf("%05d %d",it->second,it->first);
                it++;
                count++;
                if(count != mp.size()){            
                    printf(" %05d
    ",it->second);
                }else{
                    printf(" -1
    ");
                }
            }
        }
        return 0;
                
    }
    
  • 相关阅读:
    频偏(转载)
    /proc/interrupts 和 /proc/stat 查看中断的情况 (转载)
    Linux2.6 内核中结构体初始化(转载)
    用grep在子目录中指定的文件类型中查找(转载)
    用C++调用C的库函数(转载)
    H.264(MPEG-4 AVC)级别(Level)、DPB 与 MaxDpbMbs 详解(转载)
    emacs在org-mode时输出pdf时,只输出为链接
    maven 学习---使用Maven构建项目
    maven 学习---Maven构建生命周期
    maven 学习---使用Maven模板创建项目
  • 原文地址:https://www.cnblogs.com/shuibeng/p/13597396.html
Copyright © 2011-2022 走看看