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 13 1 000007 James 85 000010 Amy 90 000001 Zoe 60Sample Output 1000001 Zoe 60 000007 James 85 000010 Amy 90Sample Input 24 2 000007 James 85 000010 Amy 90 000001 Zoe 60 000002 James 98Sample Output 2000010 Amy 90 000002 James 98 000007 James 85 000001 Zoe 60Sample Input 34 3 000007 James 85 000010 Amy 90 000001 Zoe 60 000002 James 90Sample Output 3000001 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 | 答案正确 | 25 | 1028 | C++ (g++ 4.7.2) | 56 | 4020 | datrilla |
测试点
测试点 | 结果 | 用时(ms) | 内存(kB) | 得分/满分 |
---|---|---|---|---|
0 | 答案正确 | 1 | 180 | 5/5 |
1 | 答案正确 | 1 | 336 | 5/5 |
2 | 答案正确 | 1 | 308 | 5/5 |
3 | 答案正确 | 1 | 340 | 2/2 |
4 | 答案正确 | 1 | 180 | 2/2 |
5 | 答案正确 | 1 | 340 | 2/2 |
6 | 答案正确 | 56 | 4020 | 4/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; }