• PAT1028. List Sorting (25)---strcmp


    题目链接为:https://www.patest.cn/contests/pat-a-practise/1028

    1028. List Sorting (25)

    Excel can sort records according to any column. Now you are supposed to imitate this function.

    Input

    Each input file contains one test case. For each case, the first line contains two integers N (<=100000) and C, where N is the number of records and C is the column that you are supposed to sort the records with. Then N lines follow, each contains a record of a student. A student's record consists of his or her distinct ID (a 6-digit number), name (a string with no more than 8 characters without space), and grade (an integer between 0 and 100, inclusive).

    Output

    For each test case, output the sorting result in N lines. That is, if C = 1 then the records must be sorted in increasing order according to ID's; if C = 2 then the records must be sorted in non-decreasing order according to names; and if C = 3 then the records must be sorted in non-decreasing order according to grades. If there are several students who have the same name or grade, they must be sorted according to their ID's in increasing order.

    Sample Input 1
    3 1
    000007 James 85
    000010 Amy 90
    000001 Zoe 60
    
    Sample Output 1
    000001 Zoe 60
    000007 James 85
    000010 Amy 90
    
    Sample Input 2
    4 2
    000007 James 85
    000010 Amy 90
    000001 Zoe 60
    000002 James 98
    
    Sample Output 2
    000010 Amy 90
    000002 James 98
    000007 James 85
    000001 Zoe 60
    
    Sample Input 3
    4 3
    000007 James 85
    000010 Amy 90
    000001 Zoe 60
    000002 James 90
    
    Sample Output 3
    000001 Zoe 60
    000007 James 85
    000002 James 90
    000010 Amy 90
    

    题目其实特别简单,应该是考察sort的使用。

    <1>由于时间的限制,输入输出最好是scanf 和 printf,否则最后一个测试点将过不去;

    <2>同样因为时间的限制,修改vector为数组(不过应该差别不会太大,主要还是<1>)。

    论基础知识扎实的重要性,strcmp的结果是返回正数或者负数,而不是返回-1和1。这个点主要用在cmp函数中。

    设这两个字符串为str1,str2,strcmp(str1,str2)的含义是:若str1=str2,则返回零;若str1<str2,则返回负数;若str1>str2,则返回正数。
     
    以下是代码
     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 #define maxn 100005
     4 #define For(I,A,B) for(int I = (A); I < (B); I++)
     5 
     6 class student
     7 {
     8     public:
     9     char id[15],name[15];
    10     int grade;
    11 };
    12 student stu[maxn];
    13 int n,c;
    14 bool cmp(const student &a,const student &b)
    15 {
    16     if(c == 1)
    17     {
    18         if(strcmp(a.id,b.id))
    19             return strcmp(a.id,b.id) < 0;
    20     }
    21     else if(c == 2)
    22     {
    23         if(strcmp(a.name,b.name))
    24             return strcmp(a.name,b.name) < 0;
    25         return strcmp(a.id,b.id) < 0;
    26     }
    27     else if(c == 3)
    28     {
    29         if(a.grade != b.grade)
    30             return a.grade < b.grade;
    31         return strcmp(a.id,b.id) < 0;
    32     }
    33 }
    34 
    35 int main()
    36 {
    37     freopen("1028.in","r",stdin);
    38     while(scanf("%d%d",&n,&c) != EOF)
    39     {
    40         //stu.clear();
    41         For(i,0,n)
    42         {
    43             scanf("%s%s%d",&stu[i].id,&stu[i].name,&stu[i].grade);//>>stu[i].id>>stu[i].name>>stu[i].grade;
    44         }
    45         sort(stu,stu +n,cmp);
    46         For(i,0,n)
    47         {
    48             printf("%s %s %d
    ",stu[i].id,stu[i].name,stu[i].grade);//cout<<stu[i].id<<" "<<stu[i].name<<" "<<stu[i].grade<<endl;
    49         }
    50     }
    51     return 0;
    52 }
    PAT1028
  • 相关阅读:
    nohup 忽略 nohup.out 输出
    elementui 发送时间格式到 gin 后端问题
    在 Ubuntu 下使用 Puppeteer 时无法加载类库 libX11-xcb.so.1
    获取注解上的注解
    tomcat 远程 debug
    使用pytorch-lightning漂亮地进行深度学习研究(转)
    CTR预估模型发展历程(转)
    pytorch重要函数介绍
    Dataset和Dataloader
    推荐系统中的ID特征为什么有用?
  • 原文地址:https://www.cnblogs.com/xiaozhuyang/p/6108992.html
Copyright © 2020-2023  润新知