身份证排序(sort函数版)

有一组身份证号,请你按照生日对它们从大到小排序,如果日期相同,则按身份证号码大小排序。身份证号码为18位数字,出生日期为第7位到第14位。

输入格式

  第1行,包含1个整数n,表示有n个身份证号;
  接下来的n行,每行一个身份证号。

输出格式

  n行,按出生日期从大到小排序后的身份证号,每行一个身份证号。

输入样例

5
466272307503271156
215856472207097978
234804580401078365
404475727700034980
710351408803093165

输出样例

404475727700034980
234804580401078365
215856472207097978
710351408803093165
466272307503271156

数据范围

N<=100,000

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

struct IDCard {
    string number;
    string birthday;
};

bool compareIDCards(const IDCard &a, const IDCard &b) {
    if (a.birthday != b.birthday) {
        return a.birthday > b.birthday;
    } else {
        return a.number > b.number;
    }
}

int main() {
    int n;
    cin >> n;

    vector<IDCard> idCards(n);

    for (int i = 0; i < n; ++i) {
        cin >> idCards[i].number;
        idCards[i].birthday = idCards[i].number.substr(6, 8);
    }

    sort(idCards.begin(), idCards.end(), compareIDCards);

    for (int i = 0; i < n; ++i) {
        cout << idCards[i].number << endl;
    }

    return 0;
}

这个比较简洁,下面代码稍微长一点。

#include<iostream>
#include<math.h>
using namespace std;
int partition(long long arr[],long long b[],int left,int right);
void quicksort(long long arr[],long long b[],int left,int right);
int main()
{
    int n;
    cin>>n;
    long long k=pow(10,12);
    long long b[100000];
    long long a[100000];
    long long c[100000];
    for (int i = 0; i < n; i++)
    {
        cin>>a[i];
        
        b[i]=(a[i]%k/10000);//这一步我用代数的方法将出生年月抽离出来,当然如果用string,好像有一个函数可以直接将部分抽离
    }
    /*for (int i = 0; i < n-1; i++)
    {
        for (int j = 0; j < n-1-i; j++)
        {
            if(b[j]<b[j+1]||b[j]==b[j+1]&&a[j]<a[j+1])
            swap(a[j],a[j+1]);
            swap(b[j],b[j+1]);
        }
        
    }*///冒泡排序效率太低,超时。
    int count=0;
    quicksort(b,a,0,n-1);
    for (int i = n-1; i>=0; i--)
    {
        if(b[i]==b[i-1]&&(i-1>=0))
        {
            count++;
            quicksort(a,b,i-1,i+count-1);//我是要确保有多个身份证的生日相同,将他们拿在一起,进行快排。
            continue;
        }
        else
        {
            for(int q=0;q<=count;q++)
            {
               cout<<a[i+count-q]<<endl;//这一步是对同样生日的人进行输出
            }
            count=0;//要将其进行重置
        }  
    }
    return 0;
}//我的快速排序中添加了一个数组,是将二者同步变化。
int partition(long long arr[],long long b[],int left,int right)
{
    long long k=arr[left];
    long long m=b[left];
    do
    {
        while((left<right)&&(arr[right]>=k))
        
           right--;
        
        if(left<right)
        {
           arr[left]=arr[right];
           b[left]=b[right];
           left++;
        }
        
        while((left<right)&&(arr[left]<k))
        
            left++;
        
        if(left<right)
        {
            arr[right]=arr[left];
            b[right]=b[left];
            right--;
        }
        
    } while (left!=right);
    arr[left]=k;b[left]=m;
    return left;
    
}
void quicksort(long long arr[],long long b[],int left,int right)
{
    if(left>=right)
    {
        return ;
    }
    else{
        int m=partition(arr,b,left,right);
        quicksort(arr,b,left,m-1);
        quicksort(arr,b,m+1,right);
        return ;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值