• 算法答疑---08:病人排队


    算法答疑---08:病人排队

    一、总结

    一句话总结:超过60岁的年龄排序,没有超过60岁的按先后次序排序:合在一起排序的时候漏写了情况

    1、超过60岁的年龄排序,没有超过60岁的按先后次序排序如何实现?

    思路一:多种情况分开讨论:a、先所有人年龄排序取出超过60的;b、然后所有人先后次序排序取出没超过60的

    思路二:合并分析:有年龄和先后次序两个变量,所以会出现四种情况,分析的时候不要漏

    2、选择情况,或者说选择情况合并最容易出现的问题是什么?

    漏分析情况

    二、1.10编程基础之简单排序-08:病人排队

    1、题目

    08:病人排队

    总时间限制: 
    1000ms
     
    内存限制: 
    65536kB
    描述

    病人登记看病,编写一个程序,将登记的病人按照以下原则排出看病的先后顺序: 
    1. 老年人(年龄 >= 60岁)比非老年人优先看病。 
    2. 老年人按年龄从大到小的顺序看病,年龄相同的按登记的先后顺序排序。 
    3. 非老年人按登记的先后顺序看病。

    输入
    第1行,输入一个小于100的正整数,表示病人的个数;
    后面按照病人登记的先后顺序,每行输入一个病人的信息,包括:一个长度小于10的字符串表示病人的ID(每个病人的ID各不相同且只含数字和字母),一个整数表示病人的年龄,中间用单个空格隔开。
    输出
    按排好的看病顺序输出病人的ID,每行一个。
    样例输入
    5
    021075 40
    004003 15
    010158 67
    021033 75
    102012 30
    样例输出
    021033
    010158
    021075
    004003
    102012
    链接:OpenJudge - 08:病人排队
    http://noi.openjudge.cn/ch0110/08/

    2、代码及解答

    1)正确代码一

     1 #include<iostream>
     2 #include<algorithm>
     3 #include<cstring>
     4 #define MAXN 110
     5 using namespace std;
     6 
     7 struct Patient{
     8     char ID[15];
     9     int age;
    10     int number;//登记的顺序 
    11 };
    12 
    13 Patient a[MAXN];
    14 
    15 bool cmp1(Patient x, Patient y)//大于60 
    16 {
    17     if(x.age == y.age)
    18         return x.number < y.number;
    19     else
    20         return x.age > y.age;
    21 }
    22 
    23 bool cmp2(Patient x, Patient y)//小于60 
    24 {
    25     return x.number < y.number;
    26 }
    27 int main()
    28 {
    29     int n;
    30     cin >> n;
    31     for(int i =1; i <= n; ++ i)
    32     {
    33         cin >> a[i].ID >> a[i].age;
    34         a[i].number = i;
    35     }
    36     sort(a+1, a+1+n, cmp1);
    37     for(int i = 1; i <= n; ++ i)
    38     {
    39         if(a[i].age >= 60)
    40             cout << a[i].ID << endl;
    41     } 
    42     sort(a+1, a+1+n, cmp2);
    43     for(int i = 1; i <= n; ++ i)
    44     {
    45         if(a[i].age < 60)
    46             cout << a[i].ID << endl;
    47     }     
    48     return 0;
    49 }

    2)错误代码二

    #include<iostream>
    #include<algorithm>
    #include<cstring>
    #define MAXN 110
    using namespace std;
    
    struct Patient{
        char ID[15];
        int age;
        int number;//登记的顺序 
    };
    
    Patient a[MAXN];
    
    bool cmp(Patient x, Patient y)
    {
        if(x.age >= 60)
        {
            if(x.age == y.age)
                return x.number < y.number;
            else
                return x.age > y.age;
         } 
         else 
             return x.number < y.number;
    }
    
    
    int main()
    {
        int n;
        cin >> n;
        for(int i =1; i <= n; ++ i)
        {
            cin >> a[i].ID >> a[i].age;
            a[i].number = i;
        }
        sort(a+1, a+1+n, cmp1);
        for(int i = 1; i <= n; ++ i)
        {
            if(a[i].age >= 60)
                cout << a[i].ID << endl;
        } 
        for(int i = 1; i <= n; ++ i)
        {
            if(a[i].age < 60)
                cout << a[i].ID << endl;
        }     
        return 0;
    }

    错误原因:这份错误代码的思路和正确代码一是一样的,但是比较函数合在一起的时候漏分析情况了。

    下面的代码就是补足所有情况的正确代码。

    3)正确代码二

     1 #include<iostream>
     2 #include<algorithm>
     3 #include<cstring>
     4 #define MAXN 110
     5 using namespace std;
     6 
     7 struct Patient{
     8     char ID[15];
     9     int age;
    10     int number;//登记的顺序 
    11 };
    12 
    13 Patient a[MAXN];
    14 
    15 bool cmp(Patient x, Patient y)
    16 {
    17     if(x.age >= 60)
    18     {
    19         if(x.age == y.age)
    20             return x.number < y.number;
    21         else
    22             return x.age > y.age;
    23      } 
    24      else 
    25          return x.number < y.number;
    26 }
    27 
    28 bool cmp1(Patient x, Patient y)
    29 {
    30     if(x.age >= 60&&y.age>=60)
    31     {
    32         if(x.age == y.age)
    33             return x.number < y.number;
    34         else
    35             return x.age > y.age;
    36      } 
    37      else if(x.age >= 60&&y.age<60){
    38          return x.age > y.age;
    39      }
    40      else if(x.age < 60&&y.age>=60){
    41          return x.age > y.age;
    42      }
    43      else 
    44          return x.number < y.number;
    45 }
    46 
    47 int main()
    48 {
    49     int n;
    50     cin >> n;
    51     for(int i =1; i <= n; ++ i)
    52     {
    53         cin >> a[i].ID >> a[i].age;
    54         a[i].number = i;
    55     }
    56     sort(a+1, a+1+n, cmp1);
    57     for(int i = 1; i <= n; ++ i)
    58     {
    59         if(a[i].age >= 60)
    60             cout << a[i].ID << endl;
    61     } 
    62     for(int i = 1; i <= n; ++ i)
    63     {
    64         if(a[i].age < 60)
    65             cout << a[i].ID << endl;
    66     }     
    67     return 0;
    68 }
     
  • 相关阅读:
    LeetCode 2006 差的绝对值为K的数对数目
    web关键字驱动
    selenium 原理
    sched linux调度 CFS
    VMware linux虚拟机如何固定IP(亲测)
    linux centos + nginx +多版本php安装 实战(使用docker)
    如何设置linux 时区(只需一条命令)
    centos 安装 mongodb 只需三步
    生成包含常用扩展的docker phpfpm镜像含 php5.6 php7.1 php7.2 的dokcerfile(亲测可用202237)
    MySQL数据文件被恶意删除后的恢复
  • 原文地址:https://www.cnblogs.com/Renyi-Fan/p/9534440.html
Copyright © 2020-2023  润新知