1028 List Sorting (25 分)
Excel can sort records according to any column. Now you are supposed to imitate this function.
Input Specification:
Each input file contains one test case. For each case, the first line contains two integers N (≤105) 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 Specification:
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
The title question is intended for students id, name, score sort, when id ascending sort c == 1 Shi, c == 2 when the same name, ascending sort by student id
When the name is not the same, small to large sort by student name, c == 3 when the score is the same, small to large sort by student id, when the score is not the same, sorting through scores of students from small to large.
I encountered a problem: Error logic, and be careful not to play more after if;
This question ideas: direct use sort function more efficiently, a start comparing the use of string types, in cin store id and grade test results in a row last time out,
Solution: Use char type array instead of the string type, as before id 0 format output by printf (% 06d before six, less than six filled with 0)
Comparison of the char type arrays is the use of strcmp (a, b) function (in <string.h> library <cstring>) As for the type of assignment char array is also to be noted that not use char a [8], char b [8 ]; a = b, a space has eight points, point b is no longer in the space 8, if you want to use a to b should be equal strcpy (a, b) (to assign a value of b) type of assignment char preferably with scanf ( "% s", a); need to add addresses reference symbol a is the address of a [0] element & a [0] a + 1 is the address of a [0] + 1 while & a whole a array address; (parts of the compiler supports such an approach & a);
Specific code implementation:
A method of string type timeout wording:
/**
char a[10]; a="hello";//这种情况容易出现,a虽然是指针,但是它已经指向在堆栈中分配的10个字符空间,
现在这个情况a又指向数据区中的hello常量,这里的指针a出现混乱,不允许!
**/
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
struct Node{
int id,grade;
char name[10];
}node[100001];
int n, c;
bool cmp(Node a, Node b){
if(c == 1){
return a.id < b.id;
}else if(c == 2){
if(strcmp(a.name, b.name) == 0)
return a.id < b.id;
return strcmp(a.name, b.name) <= 0;
} else if(c == 3){
if(a.grade == b.grade)
return a.id < b.id;
return a.grade <= b.grade;
}
// }else{ //这种错误原因的分析 是由于 当name 和 grade 其中一个不满足时导致了
// //两个条件都进行了 a.id < b.id 判定, 判断应该只有其中一个不满足的
// return a.id < b.id;
// }
}
int main(){
int id, grade;
char name[8];
scanf("%d%d", &n, &c);
for(int i = 0; i < n; i++){
scanf("%d%s%d", &id, name, &grade);
node[i].id = id; strcpy(node[i].name, name); node[i].grade = grade;
}
sort(node, node + n, cmp);
for(int i = 0; i < n; i++){
printf("%06d %s %d\n",node[i].id, node[i].name,node[i].grade);
}
return 0;
}
The second char, scanf:
Specific code implementation:
#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
const int maxn = 100001;
struct NODE {
int no, score;
char name[10];
}node[maxn];
int c;
int cmp1(NODE a, NODE b) {
if(c == 1) {
return a.no < b.no;
} else if(c == 2) {
if(strcmp(a.name, b.name) == 0) return a.no < b.no;
return strcmp(a.name, b.name) <= 0;
} else {
if(a.score == b.score) return a.no < b.no;
return a.score <= b.score;
}
}
int main() {
int n;
scanf("%d%d", &n, &c);
for(int i = 0; i < n; i++)
scanf("%d %s %d", &node[i].no, node[i].name, &node[i].score);
sort(node, node + n, cmp1);
for(int i = 0; i < n; i++)
printf("%06d %s %d\n", node[i].no, node[i].name, node[i].score);
return 0;
}