C:(100分)
C的话是老老实实地用正序快排
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
struct One
{
char name[21];
int year = 0;
int month = 0;
int day = 0;
int ID = 0;
}allpeople[110];
void myswap(int i, int j)
{
One a;
a = allpeople[i];
allpeople[i] = allpeople[j];
allpeople[j] = a;
}
void mysort(int x, int y) //正序 快排
{
int i = x;
int j = y;
int zhong = (x + y) / 2;
One k = allpeople[zhong];
while (i <= j)
{
while (allpeople[j].year > k.year || (allpeople[j].year == k.year && allpeople[j].month > k.month) || (allpeople[j].year == k.year && allpeople[j].month == k.month && allpeople[j].day > k.day) || (allpeople[j].year == k.year && allpeople[j].month == k.month && allpeople[j].day == k.day && allpeople[j].ID < k.ID))
{
j--;
}
while (allpeople[i].year < k.year || (allpeople[i].year == k.year && allpeople[i].month < k.month) || (allpeople[i].year == k.year && allpeople[i].month == k.month && allpeople[i].day < k.day) || (allpeople[i].year == k.year && allpeople[i].month == k.month && allpeople[i].day == k.day && allpeople[i].ID > k.ID))
{
i++;
}
if (i <= j)
{
myswap(i, j);
i++;
j--;
}
}
if (x < j)
{
mysort(x, j);
}
if (y > i)
{
mysort(i, y);
}
return;
}
int main()
{
int n = 0;
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
scanf("%s %d %d %d", &allpeople[i].name, &allpeople[i].year, &allpeople[i].month, &allpeople[i].day);
allpeople[i].ID = i;
}
mysort(0, n - 1);
for (int i = 0; i < n; i++)
{
printf("%s\n", allpeople[i].name);
}
return 0;
}
C++:(100分)
C++的话是参考了题解区一位大佬的思路:
把年+月+日+ID 摞成一个数字
再配合map容器的应用
(map容器会自动对key值进行正序)
#include <iostream>
#include <map>
#include <string>
#include <math.h>
using namespace std;
int main()
{
int n = 0;
map<int, string> map1; //创建一个map容器,容器名为 map1
cin >> n;
for (int i = 0; i < n; i++)
{
string name = "";
int num = 0;
int num_swap = 0;
cin >> name;
for (int i = 1; i <= 3; i++)
{
cin >> num_swap;
num += num_swap * (100000000 / pow(10, i * 2));
}
num += 99 - i; //这步解决了: 如果有两个同学生日相同,输入靠后的同学先输出)
map1.insert(pair<int, string>(num, name));
}
//遍历
for (map<int, string>::iterator it = map1.begin(); it != map1.end(); it++)
{
cout << it->second << endl;
}
return 0;
}
python:(100分)
n = eval(input())
dian = {} # 创建一个空字典
for i in range(n):
all = input().split(" ")
name = all[0] # 名字
num = int(all[1]) * 1000000 + int(all[2]) * 10000 + int(all[3]) * 100 + (99 - i) # 最后的数字
dian[str(num)] = name # 在字典中添加元素, key是数字, value是名字
finally1 = sorted(dian.items(), key = lambda x:x[0])
'''
sorted 可以有三个参数
第一个参数:iterable(可迭代对象)(在这里是 dian.items())
就是选择你要排序的对象
然后这里用 dian.items() 先对字典进行处理
dian.items() 会把字典中每对 key 和 value 组成一个元组,再把这些元组放在一个 列表 中返回
所以 dian.items() 最后会返回一个列表
第二个参数:key(函数)(在这里是 key = lambda x:x[0])
这个参数的作用是命令 什么方式(函数) 来排序
lambda 用于定义匿名函数,也可以叫表达式
在sorted中有一种特别的准则:排序
建议观看:
https://blog.csdn.net/qq_40089648/article/details/89022804
lambda x:x[0] 中简单理解:
lambda:方法名,不用变
x:可以在允许范围内随便取名,只要与后面的 x[] 的 x 相同就好了
x[]:[]内填写要依据排序的元素,如0则是根据key值进行排序,1则是根据value值进行排序
'''
for i in finally1:
print(i[1])