PAT A1028 List Sorting (25分)

原题

Excel can sort records according to any column. Now you are supposed to imitate this function.

Input Specification:
Each input file contains one test case. For each case, the first line contains two integers N (≤10​5​​ ) and C, where N is the number of records and C is the column that you are supposed to sort the records with. Then N lines follow, each contains a record of a student. A student’s record consists of his or her distinct ID (a 6-digit number), name (a string with no more than 8 characters without space), and grade (an integer between 0 and 100, inclusive).

Output Specification:
For each test case, output the sorting result in N lines. That is, if C = 1 then the records must be sorted in increasing order according to ID’s; if C = 2 then the records must be sorted in non-decreasing order according to names; and if C = 3 then the records must be sorted in non-decreasing order according to grades. If there are several students who have the same name or grade, they must be sorted according to their ID’s in increasing order.

Sample Input 1:

3 1
000007 James 85
000010 Amy 90
000001 Zoe 60

Sample Output 1:

000001 Zoe 60
000007 James 85
000010 Amy 90

Sample Input 2:

4 2
000007 James 85
000010 Amy 90
000001 Zoe 60
000002 James 98

Sample Output 2:

000010 Amy 90
000002 James 98
000007 James 85
000001 Zoe 60

Sample Input 3:

4 3
000007 James 85
000010 Amy 90
000001 Zoe 60
000002 James 90

Sample Output 3:

000001 Zoe 60
000007 James 85
000002 James 90
000010 Amy 90

大体翻译

类似于excel中的排序,排序的类型由第二个输入的数字C决定
当C为1时,则是根据第一列递增排序
当C为2时,则是根据第二列非递减排序
当C为3时,则是根据第三列非递减排序(注意审题)
每一份数据包括三个数据段,即学号,姓名和成绩
输入要排序的数据的数量N和上述所说的C
然后输入N个数据
最后输出排序完成后的结果即可

思路

一开始,用结构体存储数据,注意因为题目要求存储105个数据,所以需要定义成全局变量,否则空间会不够
然后根据C设置三个排序最后输出就可以了
第一次编程的时候使用了冒泡排序,以及cin,cout输入输出
第二次编程的时候使用了冒泡排序,以及scanf,printf输入输出
第三次编程的时候使用了sort函数,以及scanf,printf输入输出

第一次编写

#include <iostream>
using namespace std;

struct student
{
	string id;
	string name;
	int cord;
}a[100000];

void sort(student a[], int n, int m)
{
	student temp;
	if (m == 1)
	{
		for (int i = 1; i <= n - 1; i++)
		{
			for (int j = 0; j < n - i; j++)
			{
				if (a[j].id > a[j + 1].id)
				{
					temp = a[j];
					a[j] = a[j + 1];
					a[j + 1] = temp;
				}
			}
		}
	}
	else if (m == 2)
	{
		for (int i = 1; i <= n - 1; i++)
		{
			for (int j = 0; j < n - i; j++)
			{
				if (a[j].name >= a[j + 1].name)
				{
					temp = a[j];
					a[j] = a[j + 1];
					a[j + 1] = temp;
				}
			}
		}
	}
	else if (m == 3)
	{
		for (int i = 1; i <= n - 1; i++)
		{
			for (int j = 0; j < n - i; j++)
			{
				if (a[j].cord >= a[j + 1].cord)
				{
					temp = a[j];
					a[j] = a[j + 1];
					a[j + 1] = temp;
				}
			}
		}
	}
	for (int i = 0; i < n; i++)
	{
		cout << a[i].id << " " << a[i].name << " " << a[i].cord << endl;
	}
}

int main()
{
	int n, c;
	cin >> n >> c;
	for (int i = 0; i < n; i++)
	{
		cin >> a[i].id >> a[i].name >> a[i].cord;
	}
	sort(a, n, c);
	return 0;
}

结果

在这里插入图片描述

第二次编写

#include <iostream>
#include <stdio.h>
#include <cstring>
using namespace std;

struct student
{
	char id[10];
	char name[10];
	int cord;
}a[100000];

void sort(student a[], int n, int m)
{
	student temp;
	if (m == 1)
	{
		for (int i = 1; i <= n - 1; i++)
		{
			for (int j = 0; j < n - i; j++)
			{
				if (strcmp(a[j].id, a[j + 1].id) > 0)
				{
					temp = a[j];
					a[j] = a[j + 1];
					a[j + 1] = temp;
				}
			}
		}
	}
	else if (m == 2)
	{
		for (int i = 1; i <= n - 1; i++)
		{
			for (int j = 0; j < n - i; j++)
			{
				if (strcmp(a[j].name, a[j + 1].name) >= 0)
				{
					temp = a[j];
					a[j] = a[j + 1];
					a[j + 1] = temp;
				}
			}
		}
	}
	else if (m == 3)
	{
		for (int i = 1; i <= n - 1; i++)
		{
			for (int j = 0; j < n - i; j++)
			{
				if (a[j].cord >= a[j + 1].cord)
				{
					temp = a[j];
					a[j] = a[j + 1];
					a[j + 1] = temp;
				}
			}
		}
	}
	for (int i = 0; i < n; i++)
	{
		//cout << a[i].id << " " << a[i].name << " " << a[i].cord << endl;
		printf("%s %s %d\n", a[i].id, a[i].name, a[i].cord);
	}
}

int main()
{
	int n, c;
	cin >> n >> c;
	for (int i = 0; i < n; i++)
	{
		//cin >> a[i].id >> a[i].name >> a[i].cord;
		scanf("%s", a[i].id);
		scanf("%s", a[i].name);
		scanf("%d", &a[i].cord);
	}
	sort(a, n, c);
	return 0;
}

结果

在这里插入图片描述

第三次编写

#include <iostream>
#include <stdio.h>
#include <cstring>
#include <algorithm>

using namespace std;

struct student
{
	char id[10];
	char name[10];
	int cord;
}a[100000];
/*
void sort(student a[], int n, int m)
{
	student temp;
	if (m == 1)
	{
		for (int i = 1; i <= n - 1; i++)
		{
			for (int j = 0; j < n - i; j++)
			{
				if (strcmp(a[j].id, a[j + 1].id) > 0)
				{
					temp = a[j];
					a[j] = a[j + 1];
					a[j + 1] = temp;
				}
			}
		}
	}
	else if (m == 2)
	{
		for (int i = 1; i <= n - 1; i++)
		{
			for (int j = 0; j < n - i; j++)
			{
				if (strcmp(a[j].name, a[j + 1].name) >= 0)
				{
					temp = a[j];
					a[j] = a[j + 1];
					a[j + 1] = temp;
				}
			}
		}
	}
	else if (m == 3)
	{
		for (int i = 1; i <= n - 1; i++)
		{
			for (int j = 0; j < n - i; j++)
			{
				if (a[j].cord >= a[j + 1].cord)
				{
					temp = a[j];
					a[j] = a[j + 1];
					a[j + 1] = temp;
				}
			}
		}
	}
	for (int i = 0; i < n; i++)
	{
		//cout << a[i].id << " " << a[i].name << " " << a[i].cord << endl;
		printf("%s %s %d\n", a[i].id, a[i].name, a[i].cord);
	}
}
*/
bool cmp1(student a, student b)
{
	if (strcmp(a.id, b.id) < 0)
		return 1;
	else
		return 0;
}

bool cmp2(student a, student b)
{
	/*
	if (strcmp(a.name, b.name) <= 0)
		return 1;
	else
		return 0;
	*/
	if (strcmp(a.name, b.name) != 0)
		return strcmp(a.name, b.name) < 0;
	else
		return strcmp(a.id,b.id) < 0;
}

bool cmp3(student a, student b)
{
	//return a.cord <= b.cord;
	if (a.cord != b.cord)
		return a.cord < b.cord;
	else
		return strcmp(a.id, b.id) < 0;
}

int main()
{
	int n, c;
	cin >> n >> c;
	for (int i = 0; i < n; i++)
	{
		//cin >> a[i].id >> a[i].name >> a[i].cord;
		scanf("%s", a[i].id);
		scanf("%s", a[i].name);
		scanf("%d", &a[i].cord);
	}
	//sort(a, n, c);
	switch (c)
	{
	case 1:sort(a, a + n, cmp1);
		break;
	case 2:sort(a, a + n, cmp2);
		break;
	case 3:sort(a, a + n, cmp3);
	}
	for (int i = 0; i < n; i++)
	{
		printf("%s %s %d\n", a[i].id, a[i].name, a[i].cord);
	}
	return 0;
}

结果

在这里插入图片描述

补充

在最新的VS中要使用scanf_s而不能是scanf,但在提交中还是使用scanf
下面记录scanf_s的用法:

char a[10];	//字符串
scanf_s("%s",a,10);		//需要输入变量的大小
int b;	//整数
scanf_s("%d",&b,4);

下面记录最基础的sort函数的用法:

bool cmp(int a,int b)
{
	return a<b;	//注意大于小于
}
int main()
{
	int a[10];
	for(int i = 0; i < 10; i++)
		scanf("%d",&a[i]);
	sort(a, a + 10, cmp);
	return 0;
}

题目中要求的是当姓名或分数一样,按照id进行排序,在第一次和第二次编写中,直接用了大于等于不对,但是不修改了

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值