首页 > 编程语言 >C++奥赛一本通排序题解

C++奥赛一本通排序题解

时间:2023-02-14 11:05:16浏览次数:58  
标签:return int 题解 cin C++ ++ score maxn 奥赛



title: C++奥赛一本通刷题记录(排序)
date: 2017-11-16
tags:

  • 一本通
  • openjudege
    categories: OI

C++奥赛一本通刷题记录(排序)

2017.11.16 By gwj1139177410

都是拿STL水的…别怪我…

  1. 谁考了第k名 ​​openjudge01​
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 110;
struct node{ int id; float score; }a[maxn];
int cmp(node a, node b){ return a.score==b.score?a.id<b.id:a.score>b.score; }
int main(){
int n, k; cin>>n>>k;
for(int i = 0; i < n; i++)cin>>a[i].id>>a[i].score;
sort(a,a+n,cmp);
cout<<a[k-1].id<<" "<<a[k-1].score<<"\n";
return 0;
}
  1. 奇数单增序列 ​​openjudge02​
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 550;
int n, a[maxn];
int main(){
int t; cin>>t;
while(t--){
cin>>a[n];
if(a[n]%2==1)n++;
}
sort(a,a+n);
for(int i = 0; i < n-1; i++)cout<<a[i]<<",";
cout<<a[n-1]<<"\n";
return 0;
}
  1. 成绩排序 ​​openjudge03​
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
const int maxn = 110;
struct node{ string name; int score; }a[maxn];
int cmp(node a, node b){ return a.score==b.score?a.name<b.name:a.score>b.score; }
int main(){
int n; cin>>n;
for(int i = 0; i < n; i++)cin>>a[i].name>>a[i].score;
sort(a,a+n,cmp);
for(int i = 0; i < n; i++)cout<<a[i].name<<" "<<a[i].score<<"\n";
return 0;
}
  1. 奖学金 ​​openjudge04​
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
const int maxn = 310;
struct node{ int id, score, chinese; }a[maxn];
int cmp(node a, node b){
if(a.score != b.score)return a.score>b.score;
if(a.chinese != b.chinese)return a.chinese>b.chinese;
return a.id<b.id;
}
int main(){
int n; cin>>n;
for(int i = 0; i < n; i++){
int x,y,z; cin>>x>>y>>z;
a[i].id=i+1; a[i].chinese=x; a[i].score=x+y+z;
}
sort(a,a+n,cmp);
for(int i = 0; i < 5; i++)cout<<a[i].id<<" "<<a[i].score<<"\n";
return 0;
}
  1. 分数线划定 ​​openjudge05​
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
const int maxn = 5050;
struct node{ int id, score; }a[maxn];
int cmp(node a, node b){
if(a.score != b.score)return a.score>b.score;
return a.id<b.id;
}
int main(){
int n, m; cin>>n>>m; m=m*1.5;
for(int i = 1; i <= n; i++)
cin>>a[i].id>>a[i].score;
sort(a+1,a+n+1,cmp);
for(int i = m+1; i <= n; i++)
if(a[i].score==a[m].score)m++;
cout<<a[m].score<<" "<<m<<"\n";
for(int i = 1; i <= m; i++)
cout<<a[i].id<<" "<<a[i].score<<"\n";
return 0;
}
  1. 整数奇偶排序 ​​openjudge06​
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
const int maxn = 20;
int a[maxn], b[maxn], an, bn;
bool cmp(int a, int b){return a>b;}
int main(){
for(int i = 1; i <= 10; i++){
int x; cin>>x;
if(x%2==1)a[an++]=x;
else b[bn++]=x;
}
sort(a,a+an,cmp);
sort(b,b+bn);
for(int i = 0; i < an; i++)cout<<a[i]<<" ";
for(int i = 0; i < bn; i++)cout<<b[i]<<" ";
return 0;
}
  1. 合影效果 ​​openjudge07​
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
using namespace std;
const int maxn = 50;
int an, bn;
float a[maxn], b[maxn];
bool cmp(float a, float b){return a>b;}
int main(){
int n; cin>>n;
for(int i = 1; i <= n; i++){
string s; cin>>s;
float x; cin>>x;
if(s[0]=='m')a[an++]=x;
else b[bn++]=x;
}
sort(a,a+an);
sort(b,b+bn,cmp);
for(int i = 0; i < an; i++)printf("%.2f ", a[i]);
for(int i = 0; i < bn; i++)printf("%.2f ", b[i]);
return 0;
}
  1. 病人排队 ​​openjudge08​
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
const int maxn = 110;
int an, bn;
struct node{ string id; int age, que; }a[maxn], b[maxn];
bool cmp1(node a, node b){ return a.age!=b.age?a.age>b.age:a.que<b.que; }
bool cmp2(node a, node b){ return a.que<b.que; }
int main(){
int n; cin>>n;
for(int i = 1; i <= n; i++){
string s; int x; cin>>s>>x;
if(x>=60){ a[an].id=s; a[an].que=i; a[an++].age=x; }
else { b[bn].id=s; b[bn].que=i; b[bn++].age=x; }
}
sort(a,a+an,cmp1);
sort(b,b+bn,cmp2);
for(int i = 0; i < an; i++)cout<<a[i].id<<"\n";
for(int i = 0; i < bn; i++)cout<<b[i].id<<"\n";
return 0;
}
  1. 明明的随机数 ​​openjudge09​
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 110;
int a[maxn];
int main(){
int n; cin>>n;
for(int i = 0; i < n; i++)cin>>a[i];
sort(a,a+n);
n = unique(a,a+n)-a;
cout<<n<<"\n";
for(int i = 0; i < n; i++)cout<<a[i]<<" ";
return 0;
}
  1. 单词排序 ​​openjudge10​
#include<iostream>
#include<algorithm>
#include<string>
#include<sstream>
using namespace std;
const int maxn = 110;
string a[maxn];
int main(){
string s; getline(cin,s);
int n = 0;
stringstream ss(s);
while(ss>>a[n])n++;
sort(a,a+n);
n = unique(a,a+n)-a;
for(int i = 0; i < n; i++)cout<<a[i]<<"\n";
return 0;
}
  1. 出现次数超过一半的数 ​​openjudge28​
//垃圾题目,浪费我一节课,我TM就桶排序了,你来打我啊
#include<iostream>
#include<algorithm>
using namespace std;
int n, a[1010];
int main(){
cin>>n;
for(int i = 1; i <= n; i++){
int x; cin>>x; a[x+100]++;
}
int t=0, tt=0;
for(int i = 1; i <= 200; i++)
if(a[i]>tt){ t=i; tt=a[i]; }
if(tt>n/2)cout<<t-100<<"\n";else cout<<"no\n";
return 0;
}
  1. 统计字符数 ​​openjudge29​
#include<iostream>
#include<string>
using namespace std;
int a[40];
int main(){
string s; getline(cin,s);
for(int i = 0; i < s.size(); i++)a[s[i]-'a']++;
int t=0, tt=0;
for(int i = 0; i < 40; i++)
if(a[i]>tt){ t=i; tt=a[i]; }
cout<<(char)('a'+t)<<" "<<tt<<"\n";
return 0;
}


标签:return,int,题解,cin,C++,++,score,maxn,奥赛
From: https://blog.51cto.com/gwj1314/6056158

相关文章

  • C++奥赛一本通刷题高精度题解
    title:C++奥赛一本通刷题记录(高精度)date:2017-11-15tags:一本通openjudegecategories:OIC++奥赛一本通刷题记录(高精度)2017.11.15Bygwj1139177410大整数加法​......
  • CodeVs天梯黄金Gold题解
    title:CodeVs天梯之Golddate:2017-12-28tags:天梯CodesVscategories:OICodeVs天梯之Gold2018.01.04Bygwj2330x01贪心​​均分纸牌​​#include<iostream>usingname......
  • CodeVs天梯钻石Diamond题解
    title:CodeVs天梯之Diamonddate:2017-12-28tags:天梯CodesVscategories:OICodeVs刷题攻略之Diamond2018.1.14Bygwj11391774100x01最短路​​Car的旅行路线​​//1.......
  • CodeVs天梯白银Silver题解
    title:CodeVs天梯之Silverdate:2017-12-28tags:天梯CodesVscategories:OICodeVs天梯之Silver2017.12.18Bygwj11391774100x01排序​​明明的随机数​​#include<iost......
  • CodeVs天梯青铜Bronze题解
    CodeVs天梯之Bronze2017.12.18Bygwj11391774100x01整数处理​​最小数和最大数​​#include<iostream>#include<algorithm>usingnamespacestd;intmain(){intn;c......
  • C++ dll实例
    动态链接库的制作:Windows桌面向导-应用程序类型:动态链接库(.dll)空项目 MyDynamicLib头文件声明函数时,在前面加上extern"C"__declspec(dllexport)1//MyDynami......
  • C++ Lib实例
    Lib文件的调用:1.生成的Lib文件和对应的头文件[MyStaticLib.h StaticLib.lib]复制到工程目录2.将2个文件[MyStaticLib.h StaticLib.lib]导入工程1#include<iost......
  • CF446C DZY Loves Fibonacci Numbers 题解和加强
    简要题意https://www.luogu.com.cn/problem/CF446C给定一个长度为\(n\)的序列\(A\),要求支持两种操作:1给定区间\((l,r)\)对这个区间内的每个数,依次加斐波那契数列......
  • C++开发原生WIN32程序
    VS2019文件-新建-项目-Windows桌面向导(C++)-桌面应用程序 空项目项目属性-高级-字符集未设置程序内所有字符串用TEXT宏包裹1#include<windows.h>23LONGWI......
  • str 学数学 题解
    Example1(str学数学)str同学因为名字里含有一个str,所以觉得字符串对于他来说太简单了,于是他开始了他的数学之旅。在旅途中str遇到了刚抽到胡桃的lyl,而lyl同学正......