水题找自信——问题 B: 特殊排序、问题 G: 中位数、PAT Ranking

每日刷题(106)

问题 B: 特殊排序

题目描述

输入一系列整数,将其中最大的数挑出,并将剩下的数进行排序。

输入

输入第一行包括1个整数N,1<=N<=1000,代表输入数据的个数。
接下来的一行有N个整数。

输出

可能有多组测试数据,对于每组数据,
第一行输出一个整数,代表N个整数中的最大值,并将此值从数组中去除,将剩下的数进行排序。
第二行将排序的结果输出。

样例输入

5
5 3 2 4 1

样例输出

5
1 2 3 4

提示
如果数组中只有一个数,当第一行将其输出后,第二行请输出"-1"。

C++代码:

#include<bits/stdc++.h>
using namespace std;


int main()
{
	int n;
	while(cin >> n)
	{
		int a[n];
		for(int i = 0; i < n; i++)
		{
			cin >> a[i];
		}
		if(n == 1)
		{
			cout << a[0] << endl;
			cout << "-1" << endl;
		}
		else 
		{
			sort(a, a + n);
			cout << a[n - 1] << endl;
			for(int i = 0; i < n - 1; i++)
			{
				cout << a[i] << " ";
			}
			cout << endl;
		}
		
	} 
	return 0;
} 

运行结果:
在这里插入图片描述

问题 G: 中位数

题目描述

中位数定义:一组数据按从小到大的顺序依次排列,处在中间位置的一个数(或最中间两个数据的平均数).
给出一组无序整数,求出中位数,如果求最中间两个数的平均数,向下取整即可(不需要使用浮点数)

输入

该程序包含多组测试数据,每一组测试数据的第一行为N,代表该组测试数据包含的数据个数,1<=N<=10000.
接着N行为N个数据的输入,N=0时结束输入

输出

输出中位数,每一组测试数据输出一行

样例输入

1
468
15
501
170
725
479
359
963
465
706
146
282
828
962
492
996
943
0

样例输出

468
501

C++代码:

#include<bits/stdc++.h>
using namespace std;

int main()
{
	int n;
	while(cin >> n)
	{
		if(n == 0)
			break;
		int a[n];
		for(int i = 0; i < n; i++)
		{
			cin >> a[i];
			
		}
		sort(a, a + n);
		if(n % 2)
		{
			cout << a[n / 2] << endl;
		}
		else
		{
			cout << (a[n / 2] + a[n / 2 - 1]) / 2 << endl;
		}
	}
	return 0;
} 

样例运行结果:
在这里插入图片描述

PAT Ranking(再玩一次)

题目描述

Programming Ability Test (PAT) is organized by the College of Computer Science and Technology of Zhejiang University. Each test is supposed to run simultaneously in several places, and the ranklists will be merged immediately after the test. Now it is your job to write a program to correctly merge all the ranklists and generate the final rank.
编程能力测试(PAT)是由浙江大学计算机科学与技术学院组织的。每个测试应该同时在几个地方运行,并且在测试之后,排名列表将立即合并。现在,你的工作是编写一个程序来正确地合并所有的排名,并生成最终的排名。

输入

Each input file contains one test case. For each case, the first line contains a positive number N (≤100), the number of test locations. Then N ranklists follow, each starts with a line containing a positive integer K (≤300), the number of testees, and then K lines containing the registration number (a 13-digit number) and the total score of each testee. All the numbers in a line are separated by a space.
每个输入文件包含一个测试用例。对于每种情况,第一行包含一个正数N(100),即考试位置的数量。然后是N份排名列表,每一份列表都以正整数K(300)、考生人数一行开头,然后是准考证号(13位数字)和考生考试总分在K行开头。一行中的所有数字用空格隔开。

输出

or each test case, first print in one line the total number of testees. Then print the final ranklist in the following format:
registration_number final_rank location_number local_rank
The locations are numbered from 1 to N. The output must be sorted in nondecreasing order of the final ranks. The testees with the same score must have the same rank, and the output must be sorted in nondecreasing order of their registration numbers.
对于每个测试用例,首先在一行中打印测试者的总数。然后以如下格式打印最终排名:
准考证号 最终排名 考场号 考场内排名
位置编号从1到n,输出必须按最终排名的非降序排序。相同分数的考生必须有相同的排名,并且输出必须按他们的准考证号的非降序排序。

输入样例

2
5
1234567890001 95
1234567890005 100
1234567890003 95
1234567890002 77
1234567890004 85
4
1234567890013 65
1234567890011 25
1234567890014 100
1234567890012 85

输出样例

9
1234567890005 1 1 1
1234567890014 1 2 1
1234567890001 3 1 2
1234567890003 3 1 2
1234567890004 5 1 4
1234567890012 5 2 2
1234567890002 7 1 5
1234567890013 8 2 3
1234567890011 9 2 4

C++代码如下:

#include<bits/stdc++.h>
using namespace std;

struct Student{
	char id[15];
	int score;
	int location_number;
	int local_rank;
}stu[30010];

bool cmp(Student a, Student b)
{
	if(a.score != b.score)
		return a.score > b.score;
	else
		return strcmp(a.id, b.id) < 0;
}

int main()
{
	int n, k, num = 0;	//num为总人数 
	cin >> n;			//总考场数 
	for(int i = 1; i <= n; i++)
	{
		cin >> k;		//该考场内的总人数 
		for(int j = 0; j < k; j++)
		{
			cin >> stu[num].id >> stu[num].score;
			stu[num].location_number = i;
			num++;
		}
		
		sort(stu + num - k, stu + num, cmp);
		stu[num - k].local_rank = 1;		//排名
		for(int j = num - k + 1; j < num; j++)
		{
			if(stu[j].score == stu[j - 1].score)
			{
				stu[j].local_rank = stu[j - 1].local_rank;
			}
			else
			{
				stu[j].local_rank = j - (num - k) + 1;
			}
		} 
		
	}
	cout << num << endl;
	sort(stu, stu + num, cmp);
	int r = 1;
	for(int i = 0; i < num; i++)
	{
		if(i > 0 && stu[i].score != stu[i - 1].score)
		{
			r = i + 1;
		}
		cout << stu[i].id << " ";
		cout << r << " " << stu[i].location_number<< " "<< stu[i].local_rank<< " " << endl;
	}
	return 0;
}

样例运行结果:
在这里插入图片描述

如果喜欢我的文章,请记得三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持,下期更精彩!!!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值