zoukankan      html  css  js  c++  java
  • A1028. List Sorting

    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

     1 #include<cstdio>
     2 #include<iostream>
     3 #include<string.h>
     4 #include<algorithm>
     5 using namespace std;
     6 typedef struct{
     7     char id[7];
     8     char name[9];
     9     int grade;
    10 }info;
    11 bool cmp1(info a, info b){
    12     return strcmp(a.id, b.id) < 0;
    13 }
    14 bool cmp2(info a, info b){
    15     if(strcmp(a.name, b.name) == 0)
    16         return strcmp(a.id, b.id) < 0;
    17     else
    18         return strcmp(a.name, b.name) < 0;
    19 }
    20 bool cmp3(info a, info b){
    21     if(a.grade == b.grade)
    22         return strcmp(a.id, b.id) < 0;
    23     else
    24         return a.grade < b.grade;
    25 }
    26 info stu[100001];
    27 int main(){
    28     int N, C;
    29     scanf("%d%d", &N, &C);
    30     for(int i = 0; i < N; i++)
    31         scanf("%s %s %d", stu[i].id, stu[i].name, &stu[i].grade);
    32     if(C == 1)
    33         sort(stu, stu + N, cmp1);
    34     else if(C == 2)
    35         sort(stu, stu + N, cmp2);
    36     else
    37         sort(stu, stu + N, cmp3);
    38     for(int i = 0; i < N; i++)
    39         printf("%s %s %d
    ", stu[i].id, stu[i].name, stu[i].grade);
    40     cin >> N;
    41     return 0;
    42 }
    View Code

    总结:

    1、简单题。只有一点以前没有注意到,在main函数里定义一个100000的数组时,竟然空间不足编译错误。其实只要把数组定义在main函数之外,作为全局变量即可。main函数内的变量在栈区,空间一般比较小,而全局变量一般不在栈区,空间较大。 当然也可以使用vector。

  • 相关阅读:
    Roofline Model与深度学习模型的性能分析
    卷积神经网络的复杂度分析
    CNN中卷积层的计算细节
    ImageNet 历届冠军最新评析:哪个深度学习模型最适合你?
    最新ICE源码编译安装
    CNN 模型压缩与加速算法综述
    YAML 与 front-matter
    VMware 虚拟机快照、克隆、磁盘扩容
    ubuntu16.04安装Nvidia显卡驱动、CUDA8.0和cudNN V6
    宏使用 Tricks
  • 原文地址:https://www.cnblogs.com/zhuqiwei-blog/p/8491200.html
Copyright © 2011-2022 走看看