0
点赞
收藏
分享

微信扫一扫

2022.5.3 15:07 洛谷P1104

上古神龙 2022-05-03 阅读 20

 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])
# 注释有时间再补

for i in finally1:
    print(i[1])
举报

相关推荐

P1104 生日

2022.3.25 22:15 洛谷P2141

洛谷p1135

洛谷p1551

洛谷P1309

洛谷 P1307

P1217(洛谷)

洛谷p1443

洛谷P1090

洛谷P2084

0 条评论