PAT乙组1015德才论234测试点未通过的问题(仅用结构体+字符数组)--参考并补充《算法笔记》

PAT乙组1015德才论234测试点未通过的问题(仅用结构体+字符数组)–参考并补充《算法笔记》

本人目前是在阅读算法笔记,提高算法能力,备战ccf,希望通过写博客的方式,监督自己,帮助大家!
这道题是对结构体数组进行分类和排序,思路比较清晰明了,但我却花费了2个小时的时间AC通过。做题中不断出现答案错误,运行超时,段错误等情况,其中近1个半小时在寻找错误(百度上没有找到我出现错误的原因,可能大家早就注意到了这一点吧哈哈),最终得以解决。

错误原因

对字符数组与string类的操作与特性不明确导致的。问题关键:字符数组无法直接进行字典序比较。

题目

宋代史学家司马光在《资治通鉴》中有一段著名的“德才论”:“是故才德全尽谓之圣人,才德兼亡谓之愚人,德胜才谓之君子,才胜德谓之小人。凡取人之术,苟不得圣人,君子而与之,与其得小人,不若得愚人。”

现给出一批考生的德才分数,请根据司马光的理论给出录取排名。

输入格式:
输入第一行给出 3 个正整数,分别为:N(≤10​^5),即考生总数;L(≥60),为录取最低分数线,即德分和才分均不低于 L 的考生才有资格被考虑录取;H(<100),为优先录取线——德分和才分均不低于此线的被定义为“才德全尽”,此类考生按德才总分从高到低排序;才分不到但德分到线的一类考生属于“德胜才”,也按总分排序,但排在第一类考生之后;德才分均低于 H,但是德分不低于才分的考生属于“才德兼亡”但尚有“德胜才”者,按总分排序,但排在第二类考生之后;其他达到最低线 L 的考生也按总分排序,但排在第三类考生之后。

随后 N 行,每行给出一位考生的信息,包括:准考证号 德分 才分,其中准考证号为 8 位整数,德才分为区间 [0, 100] 内的整数。数字间以空格分隔。

输出格式:
输出第一行首先给出达到最低分数线的考生人数 M,随后 M 行,每行按照输入格式输出一位考生的信息,考生按输入中说明的规则从高到低排序。当某类考生中有多人总分相同时,按其德分降序排列;若德分也并列,则按准考证号的升序输出。

输入样例:

14 60 80
10000001 64 90
10000002 90 60
10000011 85 80
10000003 85 80
10000004 80 85
10000005 82 77
10000006 83 76
10000007 90 78
10000008 75 79
10000009 59 90
10000010 88 45
10000012 80 100
10000013 90 99
10000014 66 60

输出样例:

12
10000013 90 99
10000012 80 100
10000003 85 80
10000011 85 80
10000004 80 85
10000007 90 78
10000006 83 76
10000005 82 77
10000002 90 60
10000014 66 60
10000008 75 79
10000001 64 90

思路及算法设计

方便之处:对结构体数组进行分类并存储在结构体中,这样直接对类进行排序即可。其他不再赘述,个人写的代码格式规范,思路清晰,可读性强,有注释,适合初学者。

样例点错误问题

234测试点答案错误
①stu.id的比较取决于id的数据类型(个人声明成char[])。再次提醒!!字符数组不可直接用运算符进行比较,C语言的字符串的实质是一个字符数组中存储的字符序列,如果直接比较大小相当于比较了两个字符串的首地址的大小,毫无意义。但是C++对string类型进行封装,对比较运算符进行了重载,所以可以直接比较和赋值。对参考下面链接

https://zhidao.baidu.com/question/209464511.html

也可以用int代替char数组表示id,8位数足够用int表示
②另外注意第三类,德大于等于才,注意等号,否则也会出现234测试点错误。
运行超时问题:string类型只能用cin,cout,但是cin,cout效率低下,大数据量的样例是无法通过,因此,仅对string使用,其他数据的输入输出仍使用scanf,printf。
段错误:数组开大点,没别的招了

代码–使用string

#include <bits/stdc++.h>
using namespace std;
int N, L, H;
struct student{
	string id;//此处声明的是string类型,因为可以直接进行字符串大小比较
	//char id[10]//也可声明字符数组
	int de;
	int cai;
	int total;
	int flag = 5;//考生类别设置默认值
	student(){}
	student(string str, int dd, int cc)
	{
		id = str;//string直接赋值即可,字符数组要用 (char *str) 以及strcpy()
		de = dd;
		cai = cc;
		total = dd + cc;
	}
}stu[100005];

bool cmp(student s1, student s2)
{
	if(s1.flag != s2.flag)//按类标签 升序 
	{
		return s1.flag < s2.flag;
	}
	else
	{
		if(s1.total != s2.total)//按总分  降序 
		{
			return s1.total > s2.total;		
		} 
		else
		{
			if(s1.de != s2.de)//按德分 降序 
			{
				return s1.de > s2.de;
			}
			else//按准考证号  升序 
			{
				return s1.id < s2.id;//若声明字符串类型必须使用return strcmp(s1.id, s2.id) < 0;
			}
		}
	}
}

int main(int argc, char *argv[]) {
	scanf("%d%d%d", &N, &L, &H);
	int cntPass = N;
	for(int i = 0; i < N; i++)
	{
		string str;//其实写成字符数组也能读入
		int d, c;
		cin >> str;
		scanf("%d %d", &d, &c);
		stu[i] = student(str, d, c);
		if(stu[i].de < L || stu[i].cai < L)
		{
			stu[i].flag = 5;
			cntPass--;		
		}
		else if(stu[i].de >= H && stu[i].cai >= H)
		{
			stu[i].flag = 1;
		} 
		else if(stu[i].de >= H && stu[i].cai < H)
		{
			stu[i].flag = 2;
		}
		else if(stu[i].de >= stu[i].cai)
		{
			stu[i].flag = 3;
		}
		else
		{
			stu[i].flag = 4;
		}
	}
	sort(stu, stu + N, cmp); 
	printf("%d\n", cntPass);
	for(int i = 0; i < cntPass; i++)
	{
		cout << stu[i].id << " ";
		printf("%d %d\n", stu[i].de, stu[i].cai);
	}
	return 0;
}

代码–使用char数组

#include <bits/stdc++.h>
using namespace std;
int N, L, H;
struct student{
	char id[10];
	int de;
	int cai;
	int total;
	int flag = 5;//考生类别 
	student(){}
	student(char* str, int dd, int cc)
	{
		strcpy(id, str);
		de = dd;
		cai = cc;
		total = dd + cc;
	}
}stu[100005];

bool cmp(student s1, student s2)
{
	if(s1.flag != s2.flag)//按类标签 升序 
	{
		return s1.flag < s2.flag;
	}
	else
	{
		if(s1.total != s2.total)//按总分  降序 
		{
			return s1.total > s2.total;		
		} 
		else
		{
			if(s1.de != s2.de)//按德分 降序 
			{
				return s1.de > s2.de;
			}
			else//按准考证号  升序 
			{
				return strcmp(s1.id, s2.id) < 0;
			}
		}
	}
}

int main(int argc, char *argv[]) {
	scanf("%d%d%d", &N, &L, &H);
	int cntPass = N;
	for(int i = 0; i < N; i++)
	{
		char str[10];
		int d, c;
		scanf("%s %d %d", str, &d, &c);
		stu[i] = student(str, d, c);
		if(stu[i].de < L || stu[i].cai < L)
		{
			stu[i].flag = 5;
			cntPass--;		
		}
		else if(stu[i].de >= H && stu[i].cai >= H)
		{
			stu[i].flag = 1;
		} 
		else if(stu[i].de >= H && stu[i].cai < H)
		{
			stu[i].flag = 2;
		}
		else if(stu[i].de >= stu[i].cai)
		{
			stu[i].flag = 3;
		}
		else
		{
			stu[i].flag = 4;
		}
		
	}
	sort(stu, stu + N, cmp); 
	printf("%d\n", cntPass);
	for(int i = 0; i < cntPass; i++)
	{
		printf("%s %d %d\n", stu[i].id, stu[i].de, stu[i].cai);
	}
	return 0;
}

甲组1062AC代码-int表示id

#include <bits/stdc++.h>
using namespace std;
const int maxn = 100005;
int N, L, H, cnt;
struct Student{
	int ID;
	int talent;
	int virtue;
	int total;
	int rank;
	int tag = 4;
	Student(){}
	Student(int id, int ta, int vv, int to)
	{
		ID = id;
		talent = ta;
		virtue = vv;
		total = to;
	}
}stu[maxn];
bool totalcmp(Student s1, Student s2)
{
	return s1.total > s2.total;
}
bool tagcmp(Student s1, Student s2)
{
	if(s1.tag != s2.tag)
	{
		return s1.tag < s2.tag;
	}
	else if(s1.total != s2.total)
	{
		return s1.total > s2.total;
	}
	else if(s1.virtue != s2.virtue)
	{
		return s1.virtue > s2.virtue;
	}
	else
	{
		return s1.ID < s2.ID;
	}
}

int main(int argc, char *argv[]) {
	scanf("%d%d%d", &N, &L, &H);
	for(int i = 0; i < N; ++i)
	{
		int id, vv, ta, to;
		scanf("%d%d%d", &id, &vv, &ta);
		if(vv < L || ta < L)
			continue;
		to = vv + ta;
		stu[cnt++] = Student(id, ta, vv, to);
	}
	/*sort(stu, stu + cnt, totalcmp);//没必要做个名次排序
	stu[0].rank = 1;
	for(int i = 1; i < cnt; ++i)
	{
		if(stu[i].total == stu[i - 1].total)
			stu[i].rank = stu[i - 1].rank;
		else
			stu[i].rank = i;
	}	*/
	for(int i = 0; i < cnt; ++i)
	{
		Student s = stu[i];
		if(s.virtue >= H && s.talent >= H)//第一类 
		{
			stu[i].tag = 1;
		}
		else if(s.virtue >= H && s.talent < H)//第二类 
		{
			stu[i].tag = 2;
		}
		else if(s.virtue < H && s.talent < H && s.virtue >= s.talent)//第三类
		{
			stu[i].tag = 3;
		} 
		else
		{
			stu[i].tag = 4;
		}
	}
	sort(stu, stu + cnt, tagcmp);
	printf("%d\n", cnt);
	for(int i = 0; i < cnt; ++i)
	{
		printf("%d %d %d\n", stu[i].ID, stu[i].virtue, stu[i].talent);
	}
	return 0;
}

如果有所帮助的话,我将会经常更新关于PAT,ccf遇到的问题!

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值