NOI题解(1.10编程基础之简单排序)

涉及的知识点:

vector容器存储结构体变量

sort自定义排序

稳定排序:冒泡排序

不稳定排序:algorithm中的sort函数,为不稳定排序

题目地址:http://noi.openjudge.cn/ch0110/

NOI题解(1.10编程基础之简单排序)_第1张图片

01:谁考了第k名

#include 
#include 
#include 
struct stu
{
	int num;
	float grade;
};
bool compare(const stu* st1,const stu* st2)
{
	return st1->grade>st2->grade;
}

int main()
{
	int n,k;
	std::cin>>n>>k;

	std::vector v;
	for(int i=0;i>num>>grade;
		student->num = num;
		student->grade = grade;
		v.push_back(student);
	}
	sort(v.begin(),v.end(),compare);

	// for(std::vector::iterator it=v.begin();it!=v.end();it++)
	// {
	// 	std::cout<<(*it)->num<<" "<<(*it)->grade<num<<" "<grade<

02:奇数单增序列

#include 
#include 
#include 
int main()
{
	int n;
	std::cin>>n;
	std::vectorvc;
	while(n--)
	{
		int num;
		std::cin>>num;
		if(num%2!=0)
			vc.push_back(num);
	}
	sort(vc.begin(),vc.end());
	bool firstF = true;
	for(int i=0;i

03:成绩排序

#include 
#include 
#include 
#include 
struct  stu
{
	std::string name;
	int grade;
};
//const表示引用变量不会在函数中被改变
bool compare(const stu& st1,const stu& st2)
{
	if(st1.grade>st2.grade)
		return 1;
	else if(st1.grade==st2.grade&&st1.name>n;
	std::vectorvc;
	while(n--)
	{
		std::string str;
		std::cin>>str;
		int grade;
		std::cin>>grade;
		stu student;
		student.name = str;
		student.grade = grade;
		vc.push_back(student);
	}
	sort(vc.begin(),vc.end(),compare);
	for(int i=0;i

04:奖学金

#include 
#include 
#include 
#include 
struct  stu
{
	int num;
	int chinese;
	int math;
	int english;
	int sum;
};
//const表示引用变量不会在函数中被改变
bool compare(const stu& st1,const stu& st2)
{
	if(st1.sum>st2.sum)
		return 1;
	else if(st1.sum == st2.sum && st1.chinese > st2.chinese)
		return 1;
	else if(st1.sum == st2.sum && st1.chinese == st2.chinese && st1.num>n;
	std::vectorvc;
	int num=0;
	while(n--)
	{
		num++;
		int chinese,math,english;
		std::cin>>chinese>>math>>english;

		stu student;
		student.num = num;
		student.chinese = chinese;
		student.math = math;
		student.english = english;
		student.sum = chinese+math+english;
		vc.push_back(student);
	}
	sort(vc.begin(),vc.end(),compare);
	for(int i=0;i<5;i++)
	{
		std::cout<

05:分数线划定

#include 
#include 
#include 
#include 
#include 
struct  stu
{
	int num;
	int score;
};
//const表示引用变量不会在函数中被改变
bool compare(const stu& st1,const stu& st2)
{
	if(st1.score>st2.score)
		return 1;
	else if(st1.score==st2.score&&st1.num>n>>legalN;
	legalN = floor((double)legalN*1.5);
	std::vectorvc;
	while(n--)
	{
		int num,score;
		std::cin>>num>>score;

		stu student;
		student.num = num;
		student.score = score;
		vc.push_back(student);
	}
	sort(vc.begin(),vc.end(),compare);
	int count = 0;
	for(int i=0;i=vc[legalN-1].score)
			count++;
		else
			break;
	}
	std::cout<

06:整数奇偶排序

#include 
#include 
#include 

//const表示引用变量不会在函数中被改变
bool compare(const int &odd1,const int &odd2)
{
	return odd1>odd2;
}
int main()
{
	std::vector odd;
	std::vector even;
	for(int i=0;i<10;i++)
	{
		int num;
		std::cin>>num;
		if(num%2==0)
		{
			even.push_back(num);
		}else
		{
			odd.push_back(num);
		}
	}
	sort(odd.begin(),odd.end(),compare);
	sort(even.begin(),even.end());
	for(int i=0;i

07:合影效果

#include 
#include 
#include 
#include 
#include 
#include 
struct  stu
{
	std::string gender;
	double high;
};

bool compare1(const stu& st1,const stu& st2)
{
	return st1.highst2.high;
}
int main()
{
	int n;
	std::cin>>n;
	std::vectorvc;
	while(n--)
	{
		std::string gender;
		std::cin>>gender;
		double high;
		std::cin>>high;

		stu student;
		student.gender = gender;
		student.high = high;
		vc.push_back(student);
	}
	sort(vc.begin(),vc.end(),compare1);
	for(int i=0;i


08:病人排队

#include 
#include 
#include 
using namespace std;
struct p
{
	string id;
	int age;
};
/*
冒泡排序是一种稳定的排序
比较相邻两个元素,交换也只发生在这两个元素之间
相等元素及时相邻比较也不会交换因此是稳定的排序方式
*/
void bubbleSort(vector

&pList) { for(int i=0;i>k; vector

old; vector

young; while(k--) { p pTemp; string id; int age; cin>>id>>age; pTemp.age = age; pTemp.id = id; if(age>=60) old.push_back(pTemp); else young.push_back(pTemp); } bubbleSort(old); for(int i=0;i



09:明明的随机数

#include 
#include 
#include 
#include 
using namespace std;
/*
生成1-1000的随机数
*/
// int getRandom()
// {
// 	srand(static_cast(time(0)));
// 	return rand()%1000+1;
// }
int main()
{
	set m_set;
	vector m_v;
	int N;
	cin>>N;
	while(N--)
	{
		int num;
		cin>>num;
		if(m_set.count(num)==0)
		{
			m_set.insert(num);
			m_v.push_back(num);
		}
	}
	sort(m_v.begin(),m_v.end());
	cout<

#include 
#include 
#include 
#include 
using namespace std;
/*
生成1-1000的随机数
*/
// int getRandom()
// {
// 	srand(static_cast(time(0)));
// 	return rand()%1000+1;
// }
int main()
{
	bool flag[1000]={0};
	vector m_v;
	int N;
	cin>>N;
	while(N--)
	{
		int num;
		cin>>num;
		if(flag[num]==false)
		{
			flag[num] = true;
			m_v.push_back(num);
		}
	}
	sort(m_v.begin(),m_v.end());
	cout<


10:单词排序

#include 
#include 
#include 
#include 
#include 
using namespace std;
void bubbleSort(vector &sList)
{
	for(int i=0;isList[j+1])
			{
				string sT;
				sT = sList[j];
				sList[j] = sList[j+1];
				sList[j+1] = sT;
			}
		}
	}
}
int main()
{
	string str;
	vector vc;
	set m_set;
	while(cin>>str)
	{
		vc.push_back(str);
	}
	bubbleSort(vc);
	for(int i=0;i



你可能感兴趣的:(OJ,NOI题解)