zoukankan      html  css  js  c++  java
  • 二、排序问题2

    2.1.4  编写cmp实现排序

    strcmp(str1,str2)函数比较两个字符串,需添加<string.h>头文件

    若str1==str2,则返回零;

    若str1<str2,则返回负数;

    若str1>str2,则返回正数。

    #include <stdio.h>
    #include <algorithm>
    #include <string.h>
    using namespace std;
    
    struct S{
        char name[100];
        int age;
        int score;
    }stu[1000];
    
    bool cmp(S a,S b) {//实现比较规则
        if(a.score != b.score) return a.score < b.score;//分数低的在前面
        int tmp = strcmp(a.name,b.name);//字符串比较函数 
        if(tmp != 0)  return tmp < 0; // 若分数相同,则名字序小者在前
        else return a.age < b.age; 
    }
    int main() {
        int n;
        while (scanf("%d",&n) != EOF){
            for(int i = 0;i < n;i ++){
                scanf("%s%d%d",&stu[i].name,&stu[i].age,&stu[i].score);
            }
            sort(stu,stu + n,cmp);
            for(int i = 0;i < n;i ++){//输出排序结果 
                printf("%s %d %d
    ",stu[i].name,stu[i].age,stu[i].score);
            }
        }
        return 0;
    }
  • 相关阅读:
    redis全量复制和部分复制
    tp5怎么使用find_in_set
    ms1
    nginx+php上传大文件配置
    培训第一天!
    PHP中使用CURL(五)
    PHP中使用CURL(四)
    PHP中使用CURL(三)
    PHP中使用CURL(二)
    PHP中使用CURL(一)
  • 原文地址:https://www.cnblogs.com/chao-zjj/p/8194768.html
Copyright © 2011-2022 走看看