算法笔记 4.1 codeup课后习题 排序

问题 A: 排序

时间限制: 1 Sec  内存限制: 32 MB

题目描述

对输入的n个数进行排序并输出。

输入

输入的第一行包括一个整数n(1<=n<=100)。 接下来的一行包括n个整数。

输出

可能有多组测试数据,对于每组数据,将排序后的n个整数输出,每个数后面都有一个空格。
每组测试数据的结果占一行。

样例输入

5
5 4 3 1 2

样例输出

1 2 3 4 5 

代码:

#include<iostream>
using namespace std;
int main(){
	int n;
	int a[100];
	while(cin>>n){
		for(int i=0;i<n;i++){
			cin>>a[i];
		}
		for(int i=0;i<n-1;i++){
			int min=i;
			for(int j=i+1;j<n;j++){
				if(a[min]>a[j])
					min=j;
			}
			if(min!=i){
				int t=a[i];
				a[i]=a[min];
				a[min]=t;
			}
		}

		for(int i=0;i<n;i++)
			cout<<a[i]<<" ";
		cout<<endl;

	}
	return 0;
}

问题 B: 特殊排序

时间限制: 1 Sec  内存限制: 32 MB

题目描述

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

输入

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

接下来的一行有N个整数。

输出

可能有多组测试数据,对于每组数据,

第一行输出一个整数,代表N个整数中的最大值,并将此值从数组中去除,将剩下的数进行排序。

第二行将排序的结果输出。

样例输入

<span style="color:#333333">5
5 3 2 4 1</span>

样例输出

<span style="color:#333333">5
1 2 3 4</span>

提示

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

代码:

#include<iostream>
using namespace std;
int main(){
	int n;
	int a[1010];
	while(cin>>n){
		for(int i=0;i<n;i++){
			cin>>a[i];
		}

		if(n==1){
			cout<<a[0]<<endl;
			cout<<"-1"<<endl;
			continue;
		}

		for(int i=0;i<n-1;i++){
			int min=i;
			for(int j=i+1;j<n;j++){
				if(a[min]>a[j])
					min=j;
			}
			if(min!=i){
				int t=a[i];
				a[i]=a[min];
				a[min]=t;
			}
		}
		cout<<a[n-1]<<endl;
		for(int i=0;i<n-1;i++)
			cout<<a[i]<<" ";
		cout<<endl;
	}
	return 0;
}

问题 C: EXCEL排序

时间限制: 1 Sec  内存限制: 32 MB

题目描述

Excel可以对一组纪录按任意指定列排序。现请你编写程序实现类似功能。

对每个测试用例,首先输出1行“Case i:”,其中 i 是测试用例的编号(从1开始)。随后在 N 行中输出按要求排序后的结果,即:当 C=1 时,按学号递增排序;当 C=2时,按姓名的非递减字典序排序;当 C=3 时,按成绩的非递减排序。当若干学生具有相同姓名或者相同成绩时,则按他们的学号递增排序。

输入

测试输入包含若干测试用例。每个测试用例的第1行包含两个整数 N (N<=100000) 和 C,其中 N 是纪录的条数,C 是指定排序的列号。以下有N行,每行包含一条学生纪录。每条学生纪录由学号(6位数字,同组测试中没有重复的学号)、姓名(不超过8位且不包含空格的字符串)、成绩(闭区间[0, 100]内的整数)组成,每个项目间用1个空格隔开。当读到 N=0 时,全部输入结束,相应的结果不要输出。

输出

对每个测试用例,首先输出1行“Case i:”,其中 i 是测试用例的编号(从1开始)。随后在 N 行中输出按要求排序后的结果,即:当 C=1 时,按学号递增排序;当 C=2时,按姓名的非递减字典序排序;当 C=3 时,按成绩的非递减排序。当若干学生具有相同姓名或者相同成绩时,则按他们的学号递增排序。

样例输入

4 1
000001 Zhao 75
000004 Qian 88
000003 Li 64
000002 Sun 90
4 2
000005 Zhao 95
000011 Zhao 75
000007 Qian 68
000006 Sun 85
4 3
000002 Qian 88
000015 Li 95
000012 Zhao 70
000009 Sun 95
0 3

样例输出

Case 1:
000001 Zhao 75
000002 Sun 90
000003 Li 64
000004 Qian 88
Case 2:
000007 Qian 68
000006 Sun 85
000005 Zhao 95
000011 Zhao 75
Case 3:
000012 Zhao 70
000002 Qian 88
000009 Sun 95
000015 Li 95

代码:

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

struct Student
{
	string no;
	string name;
	int grade;
	
}stu[100010];

bool cmp1(Student a,Student b){
	return a.no<b.no;
}

bool cmp2(Student a,Student b){
	if(a.name!=b.name)
		return a.name<b.name;
	return a.no<b.no;
}

bool cmp3(Student a,Student b){
	if(a.grade!=b.grade)
		return a.grade<b.grade;
	return a.no<b.no;
}

int main(){
	int n,c;
	int count=1;
	while(cin>>n>>c){
		if(n==0) break;
		for(int i=0;i<n;i++){
			cin>>stu[i].no>>stu[i].name>>stu[i].grade;
		}
		if(c==1) sort(stu,stu+n,cmp1);
		else if(c==2) sort(stu,stu+n,cmp2);
		else if(c==3) sort(stu,stu+n,cmp3);
		cout<<"Case "<<count++<<":"<<endl;
		for(int i=0;i<n;i++){
			cout<<stu[i].no<<" "<<stu[i].name<<" "<<stu[i].grade<<endl;
		}
	}
	return 0;
}

问题 D: 字符串内排序

时间限制: 1 Sec  内存限制: 32 MB

题目描述

输入一个字符串,长度小于等于200,然后将输出按字符顺序升序排序后的字符串。

输入

测试数据有多组,输入字符串。

输出

对于每组输入,输出处理后的结果。

样例输入

<span style="color:#333333">tianqin</span>

样例输出

<span style="color:#333333">aiinnqt</span>

提示

注意输入的字符串中可能有空格。

代码:

#include<iostream>
#include <string>
#include<algorithm>
using namespace std;
int main(){
	string s;
	while(getline(cin,s)){
		for(int i=0;i<s.length()-1;i++){
			int min=i;
			for(int j=i+1;j<s.length();j++){
				if(s[min]>s[j])
					min=j;
			}
			if(min!=i){
				char c=s[min];
				s[min]=s[i];
				s[i]=c;
			}
		}
		cout<<s<<endl;
	}
	return 0;
}

问题 E: Problem B

时间限制: 1 Sec  内存限制: 32 MB

题目描述

请写一个程序,对于一个m行m列的(1<m<10)的方阵,求其每一行,每一列及主对角线元素之和,最后按照从大到小的顺序依次输出。

输入

共一组数据,输入的第一行为一个正整数,表示m,接下来的m行,每行m个整数表示方阵元素。

输出

从大到小排列的一行整数,每个整数后跟一个空格,最后换行。

样例输入

4
15 8 -2 6
31 24 18 71
-3 -9 27 13
17 21 38 69

样例输出

159 145 144 135 81 60 44 32 28 27

注:原题描述有误,是主次对角线都要算,而不是仅仅主对角线,在样例输出上有体现

代码:

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
bool cmp(int a,int b){
	return a>b;
}
int main(){
	int m;
	int a[11][11];
	int sum[20];
	while(cin>>m){
		for(int i=0;i<m;i++){
			for(int j=0;j<m;j++){
				cin>>a[i][j];
			}
		}
		int index=0;
		memset(sum,0,sizeof(sum));
		//行
		for(int i=0;i<m;i++){
			for(int j=0;j<m;j++){
				sum[index]+=a[i][j];
			}
			index++;
		}
		//列
		for(int j=0;j<m;j++){
			for(int i=0;i<m;i++){
				sum[index]+=a[i][j];
			}
			index++;
		}
		//主对角线
		for(int i=0;i<m;i++){
			sum[index]+=a[i][i];
		}
		index++;
		//次对角线
		for(int i=0;i<m;i++){
			sum[index]+=a[i][m-i-1];
		}
		index++;
		sort(sum,sum+index,cmp);
		for(int i=0;i<index;i++){
			cout<<sum[i]<<" ";
		}
		cout<<endl;
	}
	return 0;
}

问题 F: 小白鼠排队

时间限制: 1 Sec  内存限制: 32 MB

题目描述

N只小白鼠(1 <= N <= 100),每只鼠头上戴着一顶有颜色的帽子。现在称出每只白鼠的重量,要求按照白鼠重量从大到小的顺序输出它们头上帽子的颜色。帽子的颜色用“red”,“blue”等字符串来表示。不同的小白鼠可以戴相同颜色的帽子。白鼠的重量用整数表示。

输入

多案例输入,每个案例的输入第一行为一个整数N,表示小白鼠的数目。

下面有N行,每行是一只白鼠的信息。第一个为不大于100的正整数,表示白鼠的重量,;第二个为字符串,表示白鼠的帽子颜色,字符串长度不超过10个字符。

注意:白鼠的重量各不相同。

输出

每个案例按照白鼠的重量从大到小的顺序输出白鼠的帽子颜色。

样例输入

1
79 omi
9
46 lcg
92 cru
37 ceq
54 vhr
17 wus
27 tnv
13 kyr
95 wld
34 qox

样例输出

omi
wld
cru
vhr
lcg
ceq
qox
tnv
wus
kyr

代码:

#include<iostream>
#include <algorithm>
using namespace std;

struct mouse
{
	int weight;
	string color;
}mou[110];

bool cmp(mouse m1,mouse m2){
	return m1.weight>m2.weight;
}

int main(){
	int n;
	while(cin>>n){
		for(int i=0;i<n;i++){
			cin>>mou[i].weight>>mou[i].color;
		}
		sort(mou,mou+n,cmp);
		for(int i=0;i<n;i++){
			cout<<mou[i].color<<endl;
		}
	}
	return 0;
}

问题 G: 中位数

时间限制: 1 Sec  内存限制: 32 MB

题目描述

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

输入

该程序包含多组测试数据,每一组测试数据的第一行为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

提示


按题目要求模拟即可

代码:

#include<iostream>
#include<algorithm>
using namespace std;
int main(){
	int n;
	int a[10010];
	while(cin>>n){
		if(n==0) break;
		for(int i=0;i<n;i++){
			cin>>a[i];
		}
		sort(a,a+n);
		if(n%2!=0) cout<<a[(n-1)/2]<<endl;
		else cout<<(a[n/2]+a[n/2-1])/2<<endl;
	}
	return 0;
}

问题 H: 整数奇偶排序

时间限制: 1 Sec  内存限制: 32 MB

题目描述

输入10个整数,彼此以空格分隔。重新排序以后输出(也按空格分隔),要求:
1.先输出其中的奇数,并按从大到小排列;
2.然后输出其中的偶数,并按从小到大排列。

输入

任意排序的10个整数(0~100),彼此以空格分隔。

输出

可能有多组测试数据,对于每组数据,按照要求排序后输出,由空格分隔。

样例输入

0 56 19 81 59 48 35 90 83 75 
17 86 71 51 30 1 9 36 14 16 

样例输出

83 81 75 59 35 19 0 48 56 90
71 51 17 9 1 14 16 30 36 86

提示


多组数据,注意输出格式


1. 测试数据可能有很多组,请使用while(cin>>a[0]>>a[1]>>...>>a[9])类似的做法来实现;

2. 输入数据随机,有可能相等。

代码:

#include<iostream>
#include<algorithm>
using namespace std;
int main(){
	int N=10;
	int even[11],odd[11];
	int x,i=0,j=0;
	while(cin>>x){
		if(x%2==0) even[i++]=x;
		else odd[j++]=x;
		if(i+j==10){
			sort(even,even+i);
			sort(odd,odd+j);
			for(int k=j-1;k>=0;k--) cout<<odd[k]<<" ";
			for(int k=0;k<i;k++) cout<<even[k]<<" ";
			cout<<endl;
			i=0,j=0;
		}
	}
	return 0;
}

问题 I: 排名

时间限制: 1 Sec  内存限制: 32 MB

题目描述

今天的上机考试虽然有实时的Ranklist,但上面的排名只是根据完成的题数排序,没有考虑每题的分值,所以并不是最后的排名。给定录取分数线,请你写程序找出最后通过分数线的考生,并将他们的成绩按降序打印。

输入

测试输入包含若干场考试的信息。每场考试信息的第1行给出考生人数N ( 0 < N < 1000 )、考题数M ( 0 < M < = 10 )、分数线(正整数)G;第2行排序给出第1题至第M题的正整数分值;以下N行,每行给出一名考生的准考证号(长度不超过20的字符串)、该生解决的题目总数m、以及这m道题的题号(题目号由1到M)。 
当读入的考生人数为0时,输入结束,该场考试不予处理。

输出

对每场考试,首先在第1行输出不低于分数线的考生人数n,随后n行按分数从高到低输出上线考生的考号与分数,其间用1空格分隔。若有多名考生分数相同,则按他们考号的升序输出。

样例输入

3 5 32
17 10 12 9 15
CS22003 5 1 2 3 4 5
CS22004 3 5 1 3
CS22002 2 1 5
0

样例输出

3
CS22003 63
CS22004 44
CS22002 32

提示


这题比较简单,计算好每个人的分数后按题目要求排序即可。

代码:

#include<iostream>
#include<algorithm>
using namespace std;

struct student
{
	string no;
	int score=0;
}stu[1010];

bool cmp(student s1,student s2){
	if(s1.score!=s2.score) return s1.score>s2.score;
	return s1.no<s2.no;
}

int main(){
	int n,m,g;
	int G[11];
	int tno,cno,index=0;//当前题号 答对题数 合格人数
	student st[1010]; 
	while(cin>>n>>m>>g){		
		if(n==0) break;
		index=0;
		for(int i=1;i<=m;i++){
			cin>>G[i];
		}
		for(int i=0;i<n;i++){
			cin>>stu[i].no>>cno;
			stu[i].score=0;
			for(int j=0;j<cno;j++){
				cin>>tno;
				stu[i].score+=G[tno];	
			}
			if(stu[i].score>=g) st[index++]=stu[i];
		}
		sort(st,st+index,cmp);
		cout<<index<<endl;
		for(int i=0;i<index;i++){
			cout<<st[i].no<<" "<<st[i].score<<endl;
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/hza419763578/article/details/82929402