码迷,mamicode.com
首页 > 其他好文 > 详细

1028 List Sorting

时间:2018-12-04 22:32:30      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:iostream   closed   algorithm   hid   include   ret   ide   cst   知识点   

这题用string和cin会超时,所以要用char。

2个小知识点。

1.id用int类型,输出时用%06d输出

2.strcmp比较char类型字符串,若str1==str2,则返回零;若str1<str2,则返回负数;若str1>str2,则返回正数。

技术分享图片
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<string.h>
#define maxn 100005
using namespace std;
typedef long long ll;
struct Node
{
    int id;
   char name[10];
    int grade;
};
bool cmp1(Node a,Node b)
{
    return a.id<b.id;
}
bool cmp2(Node a,Node b)
{
    if(strcmp(a.name,b.name)==0)
        return a.id<b.id;
    else if(strcmp(a.name,b.name)<0)
        return 1;
    else
        return 0;
}
bool cmp3(Node a,Node b)
{
    if(a.grade==b.grade)
        return a.id<b.id;
    return a.grade<b.grade;
}
int main()
{
    int n,c;
    Node no[maxn];
    scanf("%d%d",&n,&c);
    for(int i=0;i<n;i++)
        scanf("%d%s%d",&no[i].id,&no[i].name,&no[i].grade);
    if(c==1)
        sort(no,no+n,cmp1);
    else if(c==2)
        sort(no,no+n,cmp2);
    else
        sort(no,no+n,cmp3);
    for(int i=0;i<n;i++)
        printf("%06d %s %d\n",no[i].id,no[i].name,no[i].grade);
    return 0;
}
View Code

 

1028 List Sorting

标签:iostream   closed   algorithm   hid   include   ret   ide   cst   知识点   

原文地址:https://www.cnblogs.com/FTA-Macro/p/10066904.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!