1028 List Sorting(排序题)

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 (≤10​5​​) 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

本题题意 对学生的id,姓名,分数进行排序, 当 c == 1时 id由小到大排序, c == 2 当 姓名相同时, 通过学生id由小到大排序

,当姓名不同时, 通过学生姓名由小到大排序, c == 3时 当分数相同时,通过学生id由小到大排序,当分数不同时, 通过学生的分数由小到大排序。

遇到问题: 错误逻辑判断,以及注意不要在if后多打;

本题思路: 直接使用sort函数事半功倍, 一开始比较使用了string类型,于 cin 存放id 和 grade 导致最后一个测试用列超时,

解决方法:使用char类型数组代替string类型, 至于id前的0 可以通过printf格式化输出(%06d  前六位,不足六位用0填充)

而char类型数组的比较是运用 strcmp(a, b) 函数 (位于<string.h>库中 <cstring>)至于char类型数组的赋值也需要注意,不能运用 char a[8], char b[8]; a = b, a已经指向8个空间,不能再指向b的8个空间了,如果要使a 于 b相等 应该使用 strcpy(a, b)(将b的值赋值给a) char类型赋值 最好用 scanf("%s", a); 不需要添加 地址引用符号 a是a[0]元素的地址&a[0]    a + 1就是 a[0] + 1的地址 而 &a是整个a数组的地址; (部分编译器也支持&a这种写法);

具体代码实现:

方法一string类型 超时写法:

/**
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;
}

第二种char ,scanf:

具体代码实现:

#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;
}

猜你喜欢

转载自blog.csdn.net/qq_41698081/article/details/91785332
今日推荐