zoukankan      html  css  js  c++  java
  • sort

    sort  头文件  #include<algorithm>
          +   using namespace std;


    1.一维数组排序
    #include<stdio.h>
    #include<stdlib.h>
    #include<algorithm>
    using namespace std;
    bool cmp(int a,int b)//如果是double型的,参数改为double a,double b
    {
        return a<b;//升序
    }
    int main()
    {
        int a[100];
        int i;
        int n;
        scanf("%d",&n);
        for(i=0;i<=n-1;i++)
            scanf("%d",a+i);
        sort(a,a+n,cmp);
        for(i=0;i<=n-1;i++)
            printf("%d ",a[i]);
    }


    2.多个字符串排序


    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<algorithm>
    using namespace std;
    bool cmp(char *a,char *b)
    {
        return strcmp(a,b)<0;
    }
    int main()
    {
        char s[100][100];
        char *p[100];
        int i;
        int n;
        scanf("%d",&n);
        getchar();
        for(i=0; i<=n-1; i++)
        {
            gets(s[i]);
            p[i]=s[i];
        }
        sort(p,p+n,cmp);
        for(i=0; i<=n-1; i++)
            printf("%s ",p[i]);
    }






    3.对结构体进行排序




    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<algorithm>
    using namespace std;
    struct node
    {
        int x;
        int y;
    }s[100];
    bool cmp(struct node p,struct node q)//如果x相等按照y从小到大排序,否则按照x从小到大排序。
    {
        if(p.x==q.x)
            return p.y<q.y;
        return p.x<q.x;
    }
    int main()
    {
        int i;
        int n;
        scanf("%d",&n);
        for(i=0; i<=n-1; i++)
            scanf("%d %d",&s[i].x,&s[i].y);
        sort(s,s+n,cmp);
        for(i=0; i<=n-1; i++)
            printf("%d %d ",s[i].x,s[i].y);
    }

  • 相关阅读:
    OTPUB知识课堂——VMware虚拟机应该如何优化
    春风十里,不如梭子鱼云安全解决方案全心为你!
    企业进行云存储,必须先搞清楚这5个问题
    OTPUB知识讲堂——如何在云计算中部署SQL
    Convertlab——营销的艺术,数字化的艺术
    腾讯云化解安全危机,开启网络安全智能时代
    11.2
    笔记
    this
    JS数据的基本类型
  • 原文地址:https://www.cnblogs.com/jk17211764/p/9677428.html
Copyright © 2011-2022 走看看