目录

  • 6084问题【★】
  • 按1的个数排序 【★】
  • 01串排序【★★】
  • 病人排队【★★】
  • 生日排序 【★★】
  • 组队 【★】
  • 严格排名 【★】

6084问题【★】

https://nanti.jisuanke.com/t/T1470

  • 题目
    任意给出一个四位数,把它重新组成一个四位的最大数和一个最小数,算出两者间的差。

  • 例如:37213721 这个数,可以重组成:73217321 和 12371237,差值为 7321-12377321−1237。

  • 输入格式
    一个四位数。

  • 输出格式
    题目中所说的差值。
    输出时每行末尾的多余空格,不影响答案正确性

样例输入
3721
样例输出
6084
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
char a[5];
bool cmp(char a,char b)
{return a>b;
}
int str_int(char a[])//将字符串转换成int
{return (a[0]-48)*1000+(a[1]-48)*100+(a[2]-48)*10+a[3]-48;
}
int main(void)
{while( scanf("%s",a) != EOF ){sort(a,a+4);//最小的数int min=str_int(a);sort(a,a+4,cmp);//最大的数int max=str_int(a);printf("%d\n",max-min);}
}

按1的个数排序 【★】

https://nanti.jisuanke.com/t/T1480

  • 题目
    有一些 01 字串,将其按 1 的个数的多少的顺序进行输出。如果 1 的数量相等,则按照出现的先后顺序排序。

  • 输入格式
    输入数据有若干行组成。第一行是一个数 n(1≤n≤100),代表串的个数。然后 n 行每一行是一个 0101 串,每个字符串长度不超过 200 。

  • 输出格式
    重新排列 01 串的顺序。使得串按题目描述的方式排序。
    输出时每行末尾的多余空格,不影响答案正确性

样例输入
6
10011111
00001101
1010101
1
0
1100
样例输出
0
1
1100
00001101
1010101
10011111
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
struct student
{char id[205];//字符串int number;//统计1的个数int n;//序号
}stu[105];
bool cmp(student a,student b)
{if(a.number==b.number)//1的个数相等则按序号大小排return a.n<b.n;return a.number<b.number;
}
int main(void)
{int n;while( scanf("%d",&n) != EOF ){for(int i=0;i<n;i++){scanf("%s",stu[i].id);stu[i].n=i;for(int j=0;j<strlen(stu[i].id);j++){if(stu[i].id[j]=='1')stu[i].number++;}}sort(stu,stu+n,cmp);for(int i=0;i<n;i++){printf("%s\n",stu[i].id);}}return 0;
}

01串排序【★★】

https://nanti.jisuanke.com/t/T1458

  • 题目
    将 01 串首先按长度排序,长度相同时,按 1的个数多少进行排序,1 的个数相同时再按 ASCII 码值排序(字典序)。

  • 输入格式
    第一行输入一个整数 n (1≤n≤100),表示字符串的个数。
    输入数据中含有一些 01 串,01 串的长度不大于 256 个字符。

  • 输出格式
    重新排列 01 串的顺序,使得串按基本描述的方式排序,然后依次输出。
    输出时每行末尾的多余空格,不影响答案正确性

样例输入
6
10011111
00001101
1010101
1
0
1100
样例输出
0
1
1100
1010101
00001101
10011111
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
struct student
{char id[205];//字符串int number;//统计1的个数int n;//字符串的长度
}stu[105];
bool cmp(student a,student b)
{if(a.n==b.n){if(a.number==b.number)return strcmp(a.id,b.id)<0;return a.number<b.number;}   return a.n<b.n;
}
int main(void)
{int n;while( scanf("%d",&n) != EOF ){for(int i=0;i<n;i++){scanf("%s",stu[i].id);stu[i].n=strlen(stu[i].id);for(int j=0;j<strlen(stu[i].id);j++){if(stu[i].id[j]=='1')stu[i].number++;}}sort(stu,stu+n,cmp);for(int i=0;i<n;i++){printf("%s\n",stu[i].id);}}return 0;
}

病人排队【★★】

https://nanti.jisuanke.com/t/T1155

  • 题目
    病人登记看病,编写一个程序,将登记的病人按照以下原则排出看病的先后顺序:
    老年人(年龄 ≥60 岁)比非老年人优先看病。
    老年人按年龄从大到小的顺序看病,年龄相同的按登记的先后顺序排序。
    非老年人按登记的先后顺序看病。
  • 输入格式
    第 1 行,输入一个小于 100 的正整数,表示病人的个数;
    后面按照病人登记的先后顺序,每行输入一个病人的信息,包括:一个长度小于 10 的字符串表示病人的 ID(每个病人的 ID 各不相同且只含数字和字母),一个整数表示病人的年龄(不超过 100岁),中间用单个空格隔开。
  • 输出格式
    按排好的看病顺序输出病人的 ID,每行一个。
    输出时每行末尾的多余空格,不影响答案正确性
样例输入
5
021075 40
004003 15
010158 67
021033 75
102012 30
样例输出
021033
010158
021075
004003
102012

思路: 先按照老年人的规则排序。这时候的排序已经是按照年龄从大到小排的。
这时候找到小于60的人,在按照年轻人的排序规则排序。

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
struct student
{char id[15];int age;int n;
}stu[105];
bool cmp(student a,student b)//老年人
{if(a.age==b.age)return a.n<b.n;return  a.age>b.age;
}
bool cmp1(student a,student b)//不是老年人
{return a.n<b.n;
}
int main(void)
{int n;int i;while( scanf("%d",&n) != EOF ){for(i=0;i<n;i++){scanf("%s %d",stu[i].id,&stu[i].age);stu[i].n=i;}sort(stu,stu+n,cmp);for( i=0;i<n;i++){if(stu[i].age<60)//找到老年人和非老年人的分界线break;}sort(stu+i,stu+n,cmp1);for( i=0;i<n;i++){printf("%s\n",stu[i].id);}}    return 0;
}

生日排序 【★★】

https://nanti.jisuanke.com/t/T1715

  • 题目:
    蒜头学院开学了,老师要统计班里每个人的生日,并按照出生日期从早到晚排序。

  • 输入格式
    第一行一个整数 n (1≤n≤100),班级班级的人数。
    接下来 n 行,每行包含一个字符串 s 和三个整数 y,m,d,表示姓名为 s的同学出生日期是 y 年 m 月 d日。
    保证所有日期合法,姓名由小写字母构成,不超过 20个字符。

  • 输出格式
    输出 n行,每行一个字符串表示姓名。如果有两个同学出生日期相同,输入靠后的同学先输出。
    输出时每行末尾的多余空格,不影响答案正确性

样例输入
3
qwb 1996 6 30
gyt 1995 7 28
wc  1996 6 30
样例输出
gyt
wc
qwb
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
struct birthday
{string name;int year;int month;int day;int id;
}stu[105];
bool cmp(birthday a,birthday b)
{if(a.year!=b.year){return a.year<b.year;}else{if(a.month!=b.month){return a.month<b.month;}else{if(a.day!=b.day){return a.day<b.day;}else{return a.id>b.id;}}}
}
int main(void)
{int n;cin>>n;for(int i=0;i<n;i++){cin>>stu[i].name>>stu[i].year>>stu[i].month>>stu[i].day;stu[i].id=i;}sort(stu,stu+n,cmp);for(int i=0;i<n;i++){cout<<stu[i].name<<endl;}return 0;
}

我其实最开始的思路是都是字符串。将年月日字符串比较。
但是不知为啥只能同过3组

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
struct student
{char name[25];char year[5];char yue[3];char ri[3];char birthday[20];int id;
}stu[1005];
bool cmp(student a,student b)
{if(strcmp(a.birthday,b.birthday)==0)return a.id>b.id;return strcmp(a.birthday,b.birthday)<0;
}
int main(void)
{int n;int i=0;while( scanf("%d",&n) != EOF ){for(i=0;i<n;i++){scanf("%s %s %s %s",stu[i].name,stu[i].year,stu[i].yue,stu[i].ri);strcpy(stu[i].birthday,stu[i].year);strcat(stu[i].birthday,stu[i].yue);strcat(stu[i].birthday,stu[i].ri);stu[i].id=i;}sort(stu,stu+n,cmp);for(i=0;i<n;i++){printf("%s %s\n",stu[i].name,stu[i].birthday);}}return 0;
}

组队 【★】

https://nanti.jisuanke.com/t/T1445

  • 题目
    花椰妹当上了某学校程序设计竞赛队的教练。现在她要将集训队内的 n 名学生两两组队。每位学生有一个能力值,只有能力值相同的两人才能组队。
    当然这些学生也可以通过做题来提升自己的能力值。每位学生每做一道题提升一点能力值。
    花椰妹想知道,这些学生最少还要做几道题才能都组上队。

  • 输入格式
    输入的第一行包含一个整数 n(2≤n≤100),并且保证是偶数。
    输入的第二行包括 n 个整数,为每个学生的能力值 输入的第二行包括 n 个整数,为每个学生的能力值 Aiii(2<= Aiii <=100)

  • 输出格式
    输出只有一个整数——这些学生至少还要做多少道题。
    输出时每行末尾的多余空格,不影响答案正确性

样例输入1
6
5 10 2 3 14 5
样例输出1
5
样例输入2
2
1 100
样例输出2
99
#include<cstdio>
#include<algorithm>
using namespace std;
int a[105];
int main(void)
{int n;int i;int number=0;while( scanf("%d",&n) != EOF ){number=0;for(i=0;i<n;i++)scanf("%d",&a[i]);sort(a,a+n);//先从小到大for(i=0;i<n;i++){if(a[i]==a[i+1]){i++;//俩数相等,抵消了}else{number+=a[i+1]-a[i];//俩数相等抵消了i++;}}printf("%d\n",number);}return 0;
}

严格排名 【★】

https://nanti.jisuanke.com/t/T3093

样例输入
5 3
1 2 3 2 4
样例输出
3
#include<cstdio>
#include<algorithm>
using namespace std;
long int a[100025];
int main(void)
{int n,k,number;scanf("%d %d",&n,&k);int i=0;for(i=0;i<n;i++){scanf("%ld",&a[i]);}sort(a,a+n);number=1;//统计去重后的数目for(i=1;i<n;i++){if(a[i]==a[i-1]){continue;}number++;if(number==k){printf("%ld\n",a[i]);return 0;}}printf("%d",a[0]);//当n=1时的特殊情况return 0;
}

sort函数的应用习题(二)相关推荐

  1. c语言sort函数排序二维数组,js 二维数组排序sort()函数

    一.按数值排序 var arr = [[1, 2, 3], [7, 2, 3], [3, 2, 3]]; arr.sort(function(x, y){ return x[0] – y[0]; }) ...

  2. c语言sort函数排序二维数组,关于C++ 的 sort 对二维数组排序。该如何解决

    关于C++ 的 sort 对二维数组排序. 有一个二维数组.假设是 N * N: 需要对某个位置的X(横向排序).或者某个位置的Y(纵向排序). 例如: 4 6 5 1 3 2 8 7 9 对 2 行 ...

  3. sort函数进行二维vector的排序

    利用一组数据来说明,sort函数进行二维vector的排序: envelopes = [[5,4],[6,4],[6,7],[2,3]] 代码如下: #include<iostream> ...

  4. Java基础——【习题二】函数练习题

    [习题二]函数 1.定义一个方法能够判断并返回两个整数的最大值,并调用自己的方法测试是否正确. package t2; public class MaxMethod{public static voi ...

  5. matlab二维数组排序函数,Matlab 用sort函数排序 二维数组

    在Matlab中排序某个向量(一维)时,可以使用sort(A),其中A为待排序的向量,如果仅是用来排序A,那么直接使用sort(A)即可, 如果排序后还需要保留原来的索引可以用返回值,即[B,ind] ...

  6. c++ sort()函数对二维数组vector排序

    c++ sort()函数对二维数组vector排序 sort (first, last) 对容器或普通数组中 [first, last) 范围内的元素进行排序,默认进行升序排序. 对于一个一维的数组, ...

  7. c语言sort函数排序二维数组,c++ - 如何使用stl sort函数根据第二列对二维数组进行排序? - 堆栈内存溢出...

    stl排序要求迭代器的rvalue作为参数传递. 如果你想使用sort函数,你必须在c ++ 11中编译并使用数组stl来存储数组. 代码如下 #include "bits/stdc++.h ...

  8. C中的qsort函数和C++中的sort函数的理解与使用

    一.qsort()函数 原型:_CRTIMP void __cdecl qsort (void*, size_t, size_t,int (*)(const void*, const void*)); ...

  9. 不可不知的STL sort函数实现原理

    sort函数一直以来被认为是快排,今天看到一篇文章,感觉自己知道的太少. 建议大家还是要去啃<STL源码剖析>,我也要去读了,先立个flag,后续1-2个月写STL源码剖析上得到的启发. ...

最新文章

  1. 天津全国计算机考试报名时间2015,天津2020年计算机等级考试报名时间汇总
  2. Angular - - ngHref、ngSrc、ngCopy/ngCut/ngPaste
  3. 宜昌市计算机一级考试真题,2018年上半年湖北省宜昌市计算机等级考试考务通知...
  4. 无线轮播android,Android无限轮播Banner的实现
  5. boost::gil::detail::convolve_2d用法的测试程序
  6. WebAssembly,开发者赢了
  7. mysql-dj数据准备-创建班级表
  8. windows2008强制卸载辅域和元数据
  9. 【SpringBoot】Spring boot 测试类 找到不到MySQL 驱动
  10. linux 软件安装方式
  11. 基于HEVC的UHD(超高清4K)视频质量评价
  12. 操作 神通数据库_神通数据库-快速入门指南 PDF 下载
  13. 【串口服务器】的桥接模式
  14. Angular JS introduce
  15. 凑个热闹 谈谈网红沈大师
  16. 【零信任落地案例】吉大正元某大型集团公司零信任实践案例
  17. 一个WEB应用的开发流程
  18. eSIM(Embedded-SIM)-嵌入式SIM卡
  19. 累涨超200%成华尔街新宠 Fastly借边缘云有望冲上“云”霄?
  20. 相机标定板可以自己打印吗_我可以从手机或相机打印多大的照片?

热门文章

  1. 模型算法-支持向量机SVM
  2. C语言博客作业05--指针
  3. Python 34(进程重点)
  4. jQuery Event.stopPropagation() 函数详解
  5. STM32 CAN 过滤器、滤波屏蔽器配置总结
  6. 决策树 bagging boosting 的区别
  7. codeblocks调用matlab,matlab engine: 在Codeblocks中使用C++调用matlab | 学步园
  8. Hyperledger Fabric 核心模块(2)configtxgen configtx.yaml配置文件
  9. C++ Primer 5th笔记(chap 14 重载运算和类型转换)类类型转换
  10. javaWeb_JSP 动态指令 forward 的程序