sort函数的应用习题(二)

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 个整数,为每个学生的能力值 A i i i(2<= A i i i <=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;
}

猜你喜欢

转载自blog.csdn.net/qq_46527915/article/details/114742112