zoukankan      html  css  js  c++  java
  • HDU1234 开门人和关门人

    问题链接HDU1234 开门人和关门人

    问题简述:参见上述链接。

    问题分析这个问题不是很困难,还是可以锻炼人处理输入输出的能力。

    解决问题时,做两个排序,就可以找出开门的人和关门的人。程序中时间转换为整数(秒单位),以便比较排序。

    程序说明这里同时给出C和C++的程序。

    C和C++的排序库程序不一样,分别是qsort()和sort,参数不同,比较程序形式上也不一样。

    一些细节还是需要注意的,C++程序中的比较函数,参数是常量和引用。

    另外,在出来格式化输入方面,还是C语言有优势,所以C++程序中使用C语言的代码处理输入。

    这两个程序都不是最佳解法,参见以下的链接。

    参考链接HDU1234 开门人和关门人(解法二)其中的程序一边读入数据一边计算开门人和关门人,省去结构数组。

    AC通过的C语言程序如下:

    /* HDU1234 开门人和关门人 */
    
    #include <stdio.h>
    #include <stdlib.h>
    
    struct node
    {
        char name[20];
        int starttime;
        int endtime;
    } record[1000];
    
    int cmp1(const void * a, const void * b)
    {
        struct node *x = (struct node *) a;
        struct node *y = (struct node *) b;
        return x->starttime - y->starttime;
    }
    
    int cmp2(const void * a, const void * b)
    {
        struct node *x = (struct node *) a;
        struct node *y = (struct node *) b;
        return y->endtime - x->endtime;
    }
    
    int main(void)
    {
        int n, m, i;
        int h, mi, s;
    
        // 读入总天数(测试组数)
        scanf("%d", &n);
        while(n--) {
            // 读入记录数
            scanf("%d", &m);
            // 读入各个记录
            for(i=0; i<m; i++) {
                scanf("%s %d:%d:%d", record[i].name, &h, &mi, &s);
                record[i].starttime = s + mi * 60 + h * 3600;
                scanf("%d:%d:%d", &h, &mi, &s);
                record[i].endtime = s + mi * 60 + h * 3600;
            }
    
            // 排序(按开门时间)
            qsort(record, m, sizeof(record[0]), cmp1);
            // 输出开门人名字
            printf("%s ",record[0].name);
            // 排序(按关门时间)
            qsort(record, m, sizeof(record[0]), cmp2);
            printf("%s
    ",record[0].name);
        }
    
        return 0;
    }

    AC通过的C++语言程序如下:

    /* HDU1234 开门人和关门人 */
    
    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    
    using namespace std;
    
    struct node
    {
        char name[20];
        int starttime;
        int endtime;
    } record[1000];
    
    bool cmp1(const node& a, const node& b)
    {
        return a.starttime < b.starttime;
    }
    
    bool cmp2(const node& a, const node& b)
    {
        return a.endtime > b.endtime;
    }
    
    int main()
    {
        int n, m;
        int h, mi, s;
    
        // 读入总天数(测试组数)
        cin >> n;
        while(n--) {
            // 读入记录数
            cin >> m;
    
            // 读入各个记录
            for(int i=0; i<m; i++) {
                scanf("%s %d:%d:%d", record[i].name, &h, &mi, &s);
                record[i].starttime = s + mi * 60 + h * 3600;
                scanf("%d:%d:%d", &h, &mi, &s);
                record[i].endtime = s + mi * 60 + h * 3600;
            }
    
            // 排序(按开门时间)
            sort(record, record + m, cmp1);
            // 输出开门人名字
            cout << record[0].name << " ";
            // 排序(按关门时间)
            sort(record, record + m, cmp2);
            cout << record[0].name << endl;
        }
    
        return 0;
    }


  • 相关阅读:
    codeforces C. No to Palindromes!
    codeforces D. Pashmak and Parmida's problem
    codeforces C. Little Pony and Expected Maximum
    codeforces D. Count Good Substrings
    codeforces C. Jzzhu and Chocolate
    codeforces C. DZY Loves Sequences
    codeforces D. Multiplication Table
    codeforces C. Painting Fence
    hdu 5067 Harry And Dig Machine
    POJ 1159 Palindrome
  • 原文地址:https://www.cnblogs.com/tigerisland/p/7564718.html
Copyright © 2011-2022 走看看