1028. List Sorting (25)

102 篇文章 0 订阅
29 篇文章 0 订阅

1028. List Sorting (25)

时间限制
200 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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
输入N 然后就有N个records,
c=1;按ID升序排序
c=2,按名字非降排序,若相等按ID排序
c=3,按分数非将排序,若相等按ID排序;
做这题的时候不知道甚么原因,一个输出for循环竟然总是丢失第一个,好像是传参0一值错误,然后我就直接不传0了; 一开始我用cin和cout,最后一个测试点超时。改成scanf和printf又vs会报告不安全。然后就滚回vc6,结果忘记ID要补零了,但是忘记补零的时候最后一个测试点可以过。
printf("%06d",id);设置宽度6,不够补零,这里没有点啊,点的话就不补零咯

评测结果

时间结果得分题目语言用时(ms)内存(kB)用户
8月01日 15:41答案正确251028C++ (g++ 4.7.2)564020datrilla

测试点

测试点结果用时(ms)内存(kB)得分/满分
0答案正确11805/5
1答案正确13365/5
2答案正确13085/5
3答案正确13402/2
4答案正确11802/2
5答案正确13402/2
6答案正确5640204/4
#include<iostream>  
#include<string.h>
#include<algorithm> 
using namespace std;
struct Student
{
  int ID;
  char name[9];
  int grade;
};
bool recordC1cmp(const Student &A, const Student &B)
{
  return A.ID < B.ID;
}
bool recordC2cmp(const Student &A, const Student &B)
{
  if (strcmp(A.name, B.name) != 0)
    return strcmp(A.name, B.name)<0;
  return A.ID < B.ID;
}
bool recordC3cmp(const Student &A, const Student &B)
{
  if (A.grade != B.grade)
    return A.grade < B.grade;
  return A.ID < B.ID;

}
void DisplaY(Student*records, int N)
{
  int Star = 0;
  for (; Star < N; Star++)
  {
    printf("%06d %s %d\n", records[Star].ID, records[Star].name, records[Star].grade);
  }
}
int main()
{
  Student*records;
  int N, C, index; 
  scanf("%d%d", &N, &C);
  records = (Student*)malloc(sizeof(Student)*N);
  for (index = 0; index < N; index++)
  {
    scanf("%d%s%d", &records[index].ID, &records[index].name, &records[index].grade);
  }
  switch (C)
  {
  case 1:sort(records, records + N, recordC1cmp); break;
  case 2:sort(records, records + N, recordC2cmp); break;
  case 3:sort(records, records + N, recordC3cmp); break;
  default:printf("error:valid data :C\n");
  }
  DisplaY(records, N);
  free(records);
  system("pause");
  return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值