洛谷 排序

P1059 明明的随机数
本题数据仅限于1000以内的正整数,规模小,适合用桶排序
太简单 不加注释

#include <bits/stdc++.h>
using namespace std;
int x[1200]={0};
int main()
{
    int n,num,res=0;
    cin>>n;
    for(int i=0; i<n; i++)
    {
        cin>>num;
        if(x[num]==0)
        {
            x[num]=1;
            res++;
        }
    }
    cout<<res<<endl;
    for(int i=0;i<1200;i++)
    {
        if(x[i]==1)
            cout<<i<<" ";
    }
    return 0;
}

P1068 分数线划定
结构体排序

#include <bits/stdc++.h>
using namespace std;
struct cj
{
    int k;
    int s;
} x[10000];
int cmp(const cj &a,const cj &b)
{
    if(a.s!=b.s)
        return a.s>b.s;
    else
        return a.k<b.k;
}
int main()
{
    int m,n;
    cin>>n>>m;
    int j,res=0;
    for(int i=0; i<n; i++)
        cin>>x[i].k>>x[i].s;
    m=m*1.5;
    sort(x,x+n,cmp);
    j=x[m-1].s;
    for(int i=0; i<n; i++)
    {
        if(x[i].s>=j)
            res++;
    }
    cout<<j<<" "<<res<<endl;
    for(int i=0; i<res; i++)
    {
        cout<<x[i].k<<" "<<x[i].s<<endl;
    }
    return 0;
}

P1781 宇宙总统
字符串比大小
字符串长度函数

#include <bits/stdc++.h>
using namespace std;
struct cj
{
    string ps;
    int id;
    int ws;
} x[25];
int cmp(const cj &a,const cj &b)
{
    if(a.ws!=b.ws)
        return a.ws>b.ws;
    else
        return a.ps>b.ps;//字符串可以逐位比大小
}
int main()
{
    int n;
    cin>>n;
    for(int i=0; i<n; i++)
    {
        cin>>x[i].ps;
        x[i].id=i+1;
        x[i].ws=x[i].ps.size();//统计票数的位数
    }
    sort(x,x+n,cmp);
    cout<<x[0].id<<endl<<x[0].ps;
    return 0;
}
发布了13 篇原创文章 · 获赞 0 · 访问量 337

猜你喜欢

转载自blog.csdn.net/qq_46126537/article/details/104056039
今日推荐