NOI題解(1.10程式設計基礎之簡單排序)
阿新 • • 發佈:2019-02-03
涉及的知識點:
vector容器儲存結構體變數
sort自定義排序
穩定排序:氣泡排序
不穩定排序:algorithm中的sort函式,為不穩定排序
題目地址:http://noi.openjudge.cn/ch0110/
01:誰考了第k名
#include <iostream> #include <vector> #include <algorithm> 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<stu*> v; for(int i=0;i<n;i++) { stu *student = new stu(); int num; float grade; std::cin>>num>>grade; student->num = num; student->grade = grade; v.push_back(student); } sort(v.begin(),v.end(),compare); // for(std::vector<stu*>::iterator it=v.begin();it!=v.end();it++) // { // std::cout<<(*it)->num<<" "<<(*it)->grade<<std::endl; // } std::cout<<(*v[k-1]).num<<" "<<(*v[k-1]).grade<<std::endl; // std::cout<<v[k-1]->num<<" "<<v[k-1]->grade<<std::endl;//這兩種訪問方式都可以 return 0; }
02:奇數單增序列
#include <iostream> #include <vector> #include <algorithm> int main() { int n; std::cin>>n; std::vector<int>vc; 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<vc.size();i++) { if(firstF) { std::cout<<vc[i]; firstF = false; } else { std::cout<<","<<vc[i]; } } return 0; }
03:成績排序
#include <iostream> #include <vector> #include <algorithm> #include <string> 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<st2.name) return 1; else return 0; } int main() { int n; std::cin>>n; std::vector<stu>vc; 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<vc.size();i++) { std::cout<<vc[i].name<<" "<<vc[i].grade<<std::endl; } return 0; }
04:獎學金
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
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<st2.num)
return 1;
else
return 0;
}
int main()
{
int n;
std::cin>>n;
std::vector<stu>vc;
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<<vc[i].num<<" "<<vc[i].sum<<std::endl;
}
return 0;
}
05:分數線劃定
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <math.h>
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<st2.num)
return 1;
else
return 0;
}
int main()
{
int n, legalN;
std::cin>>n>>legalN;
legalN = floor((double)legalN*1.5);
std::vector<stu>vc;
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.size();i++)
{
if(vc[i].score>=vc[legalN-1].score)
count++;
else
break;
}
std::cout<<vc[legalN-1].score<<" "<<count<<std::endl;
for(int i=0;i<count;i++)
{
std::cout<<vc[i].num<<" "<<vc[i].score<<std::endl;
}
return 0;
}
06:整數奇偶排序
#include <iostream>
#include <vector>
#include <algorithm>
//const表示引用變數不會在函式中被改變
bool compare(const int &odd1,const int &odd2)
{
return odd1>odd2;
}
int main()
{
std::vector<int> odd;
std::vector<int> 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<odd.size();i++)
{
std::cout<<odd[i]<<" ";
}
for(int i=0;i<even.size();i++)
{
std::cout<<even[i]<<" ";
}
return 0;
}
07:合影效果
#include <iostream>
#include <vector>
#include <algorithm>
#include <string.h>
#include <math.h>
#include <iomanip>
struct stu
{
std::string gender;
double high;
};
bool compare1(const stu& st1,const stu& st2)
{
return st1.high<st2.high;
}
//const表示引用變數不會在函式中被改變
bool compare2(const stu& st1,const stu& st2)
{
return st1.high>st2.high;
}
int main()
{
int n;
std::cin>>n;
std::vector<stu>vc;
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<vc.size();i++)
{
if(!strcmp(vc[i].gender.data(),"male"))
std::cout<<std::fixed<<std::setprecision(2)<<vc[i].high<<" ";
}
sort(vc.begin(),vc.end(),compare2);
for(int i=0;i<vc.size();i++)
{
if(!strcmp(vc[i].gender.data(),"female"))
std::cout<<vc[i].high<<" ";
}
return 0;
}
08:病人排隊
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct p
{
string id;
int age;
};
/*
氣泡排序是一種穩定的排序
比較相鄰兩個元素,交換也只發生在這兩個元素之間
相等元素及時相鄰比較也不會交換因此是穩定的排序方式
*/
void bubbleSort(vector<p> &pList)
{
for(int i=0;i<pList.size()-1;i++)
{
for(int j=0;j<pList.size()-1-i;j++)
{
if(pList[j].age < pList[j+1].age)
{
//swap
p pT;
pT = pList[j];
pList[j] = pList[j+1];
pList[j+1] = pT;
}
}
}
}
int main()
{
int k;
cin>>k;
vector<p> old;
vector<p> 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<old.size();i++)
{
cout<<old[i].id<<" "<<endl;
}
for(int i=0;i<young.size();i++)
{
cout<<young[i].id<<" "<<endl;
}
return 0;
}
09:明明的隨機數
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
/*
生成1-1000的隨機數
*/
// int getRandom()
// {
// srand(static_cast<unsigned int>(time(0)));
// return rand()%1000+1;
// }
int main()
{
set<int> m_set;
vector<int> 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<<m_v.size()<<endl;
for(int i=0;i<m_v.size();i++)
{
cout<<m_v[i]<<" ";
}
return 0;
}
或
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
/*
生成1-1000的隨機數
*/
// int getRandom()
// {
// srand(static_cast<unsigned int>(time(0)));
// return rand()%1000+1;
// }
int main()
{
bool flag[1000]={0};
vector<int> 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<<m_v.size()<<endl;
for(int i=0;i<m_v.size();i++)
{
cout<<m_v[i]<<" ";
}
return 0;
}
10:單詞排序
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <string.h>
using namespace std;
void bubbleSort(vector<string> &sList)
{
for(int i=0;i<sList.size()-1;i++)
{
for(int j=0;j<sList.size()-1-i;j++)
{
if(sList[j]>sList[j+1])
{
string sT;
sT = sList[j];
sList[j] = sList[j+1];
sList[j+1] = sT;
}
}
}
}
int main()
{
string str;
vector<string> vc;
set<string> m_set;
while(cin>>str)
{
vc.push_back(str);
}
bubbleSort(vc);
for(int i=0;i<vc.size();i++)
{
if(m_set.count(vc[i]))
{
continue;
}else
{
m_set.insert(vc[i]);
cout<<vc[i]<<endl;
}
}
return 0;
}