华为机试C语言-免单统计

题目描述:https://pycoder.blog.csdn.net/article/details/124677562

非常好用的qsort

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

struct Node {
    int year;
    int month;
    int date;
    int hours;
    int minute;
    int second;
    int ms;
};

int myCmp(const void *a, const void *b)
{
    struct Node *aa = (struct Node *)a;
    struct Node *bb = (struct Node *)b;
    
    if (aa->year != bb->year) return aa->year - bb->year;
    if (aa->month != bb->month) return aa->month - bb->month;
    if (aa->date != bb->date) return aa->date - bb->date;
    if (aa->hours != bb->hours) return aa->hours - bb->hours;
    if (aa->minute != bb->minute) return aa->minute - bb->minute;
    if (aa->second != bb->second) return aa->second - bb->second;
    if (aa->ms != bb->ms) return aa->ms - bb->ms;
    
    return 0;
}

int main(void)
{
    int n;
    char str[100] = {0};
    int index = 0;
    struct Node node[1000];
    int res = 0;
    
    scanf("%d", &n);
    getchar();
    
    for (int i = 0; i < n; i++) {
        gets(str);
        sscanf(str, "%d-%d-%d %d:%d:%d.%d", &node[index].year, &node[index].month, &node[index].date,
              &node[index].hours, &node[index].minute, &node[index].second, &node[index].ms);
        
        index++;  
    }
    
    qsort(node, index, sizeof(node[0]), myCmp);
    
    struct Node pre = node[0];
    res = 1;
    for (int i = 1; i < index; i++) {
        if (node[i].year != pre.year) goto lable;
        if (node[i].month != pre.month) goto lable;
        if (node[i].date != pre.date) goto lable;
        if (node[i].hours != pre.hours) goto lable;
        if (node[i].minute != pre.minute) goto lable;
        if (node[i].second != pre.second) goto lable;
        if (node[i].ms == pre.ms) goto lable;

        continue;
        
lable:
        pre = node[i];
        res++;
    }
    
    printf("%d\n", res);
    
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值