前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >模拟EXCEL排序 c++ sort排序 多重排序 题解

模拟EXCEL排序 c++ sort排序 多重排序 题解

作者头像
十四君
发布2019-11-28 16:48:39
1.2K0
发布2019-11-28 16:48:39
举报
文章被收录于专栏:Urlteam

题目链接: https://pta.patest.cn/pta/test/15/exam/4/question/864附录有strcmp函数使用以及多重sort的解析.

5-37 模拟EXCEL排序   (25分)

Excel可以对一组纪录按任意指定列排序。现请编写程序实现类似功能。

输入格式:

输入的第一行包含两个正整数NN(\le 10^5≤10​5​​) 和CC,其中NN是纪录的条数,CC是指定排序的列号。之后有 NN行,每行包含一条学生纪录。每条学生纪录由学号(6位数字,保证没有重复的学号)、姓名(不超过8位且不包含空格的字符串)、成绩([0, 100]内的整数)组成,相邻属性用1个空格隔开。

输出格式:

在NN行中输出按要求排序后的结果,即:当C=1C=1时,按学号递增排序;当C=2C=2时,按姓名的非递减字典序排序;当C=3C=3时,按成绩的非递减排序。当若干学生具有相同姓名或者相同成绩时,则按他们的学号递增排序。

输入样例:

代码语言:javascript
复制
3 1
000007 James 85
000010 Amy 90
000001 Zoe 60

输出样例:

代码语言:javascript
复制
000001 Zoe 60
000007 James 85
000010 Amy 90

源代码 : 函数解析在注释

代码语言:javascript
复制
#include<iostream>
#include<cstdlib>
#include "stdio.h"
#include "string.h"
#include<algorithm>
using namespace std;
struct test{
    int a;
    int b;
    char name[10];
};
bool cmp_num(const test &x,const test &y) {  //只排学号
    return x.a<y.a;   
}
bool cmp_name(const test &x,const test &y) { //根据name的asc码比较
    //printf("%s %s %d\n", x.name , y.name ,strcmp(x.name , y.name));//返回值是根据asc码的所以返回值有正负和0,并不是直接返回01作为大小值
    if(strcmp(x.name , y.name) > 0 )    return 0;
    else return 1;
}
bool cmp_xuehao(const test &x,const test &y) { //二重排序 满足某条件则 
    if(strcmp(x.name, y.name) == 0 || x.b == y.b) return x.a<y.a;//满足条件进行比较.sort本质上是根据cmp返回的是01值来正排与倒序
    else return x.b<y.b;
}
int main()
{
    int n,c;
    test x[100006];
    scanf("%d%d", &n,&c);
    for (int i = 0; i < n; i++) {
        scanf("%6d %s %d", &x[i].a, x[i].name, &x[i].b);
    }
    if(c==1)sort(x,x+n,cmp_num);
    else if(c == 2)sort(x,x+n,cmp_name);
    else if(c == 3)sort(x,x+n,cmp_xuehao);//sort排序
 
    for (int i = 0; i < n; i++) {
        printf("%06d %s %d\n", x[i].a, x[i].name, x[i].b);
    }
    return 0;
}

函数名: strcmp

代码语言:javascript
复制
   功 能: 串比较
  用 法: int strcmp(char *str1, char *str2);
  看Asic码,str1>str2,返回值 > 0;两串相等,返回0
  程序例:
  #include <string.h>
  #include <stdio.h>
  int main(void)
  {
  char *buf1 = "aaa", *buf2 = "bbb", *buf3 = "ccc";
  int ptr;
  ptr = strcmp(buf2, buf1);
  if (ptr > 0)
  printf("buffer 2 is greater than buffer 1\n");
  else
  printf("buffer 2 is less than buffer 1\n");
  ptr = strcmp(buf2, buf3);
  if (ptr > 0)
  printf("buffer 2 is greater than buffer 3\n");
  else
  printf("buffer 2 is less than buffer 3\n");
  return 0;
  }

参考代码: sort多重排序样例

代码语言:javascript
复制
#include<iostream>
#include<cstdlib>
#include<algorithm>
using namespace std;
struct test{
    int a;
    int b;
    test():a(0),b(0){}
    test(int x,int y=0):a(x),b(y){}
    set(int x,int y){a=x;b=y;}
};
bool cmp(const test &x,const test &y)
{
    if(x.a != y.a)
        return x.a<y.a;
    else
        return x.b<y.b;
}
int main()
{
    test x[10];
    for(int i=0;i<10;i++)
    {
        int m = rand()%100;
        int n = rand()%100;
        x[i].set(m,n);
    }
    cout<<"before sorted"<<endl;
    for(int i=0;i<10;i++)
    {
        cout<<x[i].a<<" "<<x[i].b<<endl;
    }
    sort(x,x+10,cmp);
    cout<<"after sorted"<<endl;
    for(int i=0;i<10;i++)
    {
        cout<<x[i].a<<" "<<x[i].b<<endl;
    }
    return 0;
}

原创文章,转载请注明: 转载自URl-team

本文链接地址: 模拟EXCEL排序 c++ sort排序 多重排序 题解

No related posts.

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016-05-252,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 5-37 模拟EXCEL排序   (25分)
    • 输入格式:
      • 输出格式:
        • 输入样例:
          • 输出样例:
          • 源代码 : 函数解析在注释
          • 函数名: strcmp
          • 参考代码: sort多重排序样例
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档