1015. 德才论 (25)
来源:互联网 发布:上海地铁刷卡数据 编辑:程序博客网 时间:2024/05/18 02:44
宋代史学家司马光在《资治通鉴》中有一段著名的“德才论”:“是故才德全尽谓之圣人,才德兼亡谓之愚人,德胜才谓之君子,才胜德谓之小人。凡取人之术,苟不得圣人,君子而与之,与其得小人,不若得愚人。”
现给出一批考生的德才分数,请根据司马光的理论给出录取排名。
输入格式:
输入第1行给出3个正整数,分别为:N(<=105),即考生总数;L(>=60),为录取最低分数线,即德分和才分均不低于L的考生才有资格被考虑录取;H(<100),为优先录取线——德分和才分均不低于此线的被定义为“才德全尽”,此类考生按德才总分从高到低排序;才分不到但德分到线的一类考生属于“德胜才”,也按总分排序,但排在第一类考生之后;德才分均低于H,但是德分不低于才分的考生属于“才德兼亡”但尚有“德胜才”者,按总分排序,但排在第二类考生之后;其他达到最低线L的考生也按总分排序,但排在第三类考生之后。
随后N行,每行给出一位考生的信息,包括:准考证号、德分、才分,其中准考证号为8位整数,德才分为区间[0, 100]内的整数。数字间以空格分隔。
输出格式:
输出第1行首先给出达到最低分数线的考生人数M,随后M行,每行按照输入格式输出一位考生的信息,考生按输入中说明的规则从高到低排序。当某类考生中有多人总分相同时,按其德分降序排列;若德分也并列,则按准考证号的升序输出。
输入样例:14 60 8010000001 64 9010000002 90 6010000011 85 8010000003 85 8010000004 80 8510000005 82 7710000006 83 7610000007 90 7810000008 75 7910000009 59 9010000010 88 4510000012 80 10010000013 90 9910000014 66 60输出样例:
1210000013 90 9910000012 80 10010000003 85 8010000011 85 8010000004 80 8510000007 90 7810000006 83 7610000005 82 7710000002 90 6010000014 66 6010000008 75 7910000001 64 90
#include <stdio.h> #include <malloc.h>struct person{ int index; int de; int cai;};void swap(person *a,person *b){ int index = a->index; int de=a->de; int cai=a->cai; a->index=b->index; a->de=b->de; a->cai=b->cai; b->index=index; b->de=de; b->cai=cai;}void order(person lels[],int count){ int i,j; for(i = 0;i < count-1;i++)for(j = i+1; j < count;j++) { if(lels[i].de + lels[i].cai< lels[j].de+ lels[j].cai)swap(&lels[i],&lels[j]); else if(lels[i].de < lels[j].de)swap(&lels[i],&lels[j]); else if(lels[i].de==lels[j].de && lels[i].index > lels[j].index)swap(&lels[i],&lels[j]); }}int main(void) { int cur=0,count=0,L=0,H=0; int c1=0,c2=0,c3=0,c4=0; scanf("%d%d%d",&cur,&L,&H); person *per1 = (person *)malloc(100000*sizeof(person)); person *per2 = (person *)malloc(100000*sizeof(person)); person *per3 = (person *)malloc(100000*sizeof(person)); person *per4 = (person *)malloc(100000*sizeof(person)); for(int i=0;i<cur;i++){ person tmp; scanf("%d%d%d",&tmp.index,&tmp.de,&tmp.cai); if(tmp.de>=L&&tmp.cai>=L){ if(tmp.de>=H&&tmp.cai>=H) per1[c1++]=tmp; else if(tmp.de>=H)per2[c2++]=tmp; else if(tmp.de>=tmp.cai)per3[c3++]=tmp; else per4[c4++]=tmp; } } printf("%d\n",c1+c2+c3+c4); order(per1,c1); order(per2,c2); order(per3,c3); order(per4,c4); for(int i=0;i<c1;i++)printf("%d %d %d\n",per1[i].index,per1[i].de,per1[i].cai); for(int i=0;i<c2;i++)printf("%d %d %d\n",per2[i].index,per2[i].de,per2[i].cai); for(int i=0;i<c3;i++)printf("%d %d %d\n",per3[i].index,per3[i].de,per3[i].cai); for(int i=0;i<c4;i++)printf("%d %d %d\n",per4[i].index,per4[i].de,per4[i].cai); return 0;}
#include <stdlib.h>#include <iostream>#include <malloc.h>using namespace std;struct student{ int num; int d; int c;};int comp(const void *a, const void *b){ if((*(student*)a).d+(*(student*)a).c != (*(student*)b).d+(*(student*)b).c) return ((*(student*)b).d+(*(student*)b).c)-((*(student*)a).d+(*(student*)a).c); else if((*(student*)a).d != (*(student*)b).d) return (*(student*)b).d-(*(student*)a).d; else return (*(student*)a).num-(*(student*)b).num;} int main(){ int n,l,h,i,count=0; int num,d,c; int n1=0,n2=0,n3=0,n4=0; scanf("%d %d %d",&n,&l,&h); student *p1 = (student *)malloc(100000*sizeof(student)); student *p2 = (student *)malloc(100000*sizeof(student)); student *p3 = (student *)malloc(100000*sizeof(student)); student *p4 = (student *)malloc(100000*sizeof(student)); student temp; for(i=0;i<n;i++) { scanf("%d %d %d",&temp.num,&temp.d,&temp.c); if((temp.d>=l)&&(temp.c>=l)) { if((temp.d>=h)&&(temp.c>=h)) p1[n1++] = temp; else if(temp.d>=h) p2[n2++] = temp; else if(temp.d>=temp.c) p3[n3++] = temp; else p4[n4++] = temp; } } qsort(p1,n1,sizeof(student),comp); qsort(p2,n2,sizeof(student),comp); qsort(p3,n3,sizeof(student),comp); qsort(p4,n4,sizeof(student),comp); printf("%d\n",n1+n2+n3+n4); for(i=0;i<n1;i++) printf("%d %d %d\n",p1[i].num,p1[i].d,p1[i].c); for(i=0;i<n2;i++) printf("%d %d %d\n",p2[i].num,p2[i].d,p2[i].c); for(i=0;i<n3;i++) printf("%d %d %d\n",p3[i].num,p3[i].d,p3[i].c); for(i=0;i<n4;i++) printf("%d %d %d\n",p4[i].num,p4[i].d,p4[i].c); system("pause"); return 0;}
这道题遇到一些问题,开数组120万的话devc会报错
百度下基本都是采用vector的方式,操作符重载然后调用sort
后来琢磨了琢磨用molloc也能实现
就是qsort的cmop函数不知道怎么才能写的更好看点
附上vector的代码:
#include <stdio.h>#include <iostream>#include <vector>#include <algorithm>using namespace std;struct student{ int num; int d; int c; bool operator <(const student &A) const{ if(d+c != A.d+A.c) return d+c>A.d+A.c; else if(d != A.d) return d>A.d; else return num<A.num; }};int main(){ int n,l,h,i,count=0; vector<student> v1,v2,v3,v4; student temp; scanf("%d %d %d",&n,&l,&h); for(i=0;i<n;i++) { scanf("%d %d %d",&temp.num,&temp.d,&temp.c); if((temp.d>=l)&&(temp.c>=l)) { count++; if((temp.d>=h)&&(temp.c>=h)) v1.push_back(temp); else if(temp.d>=h) v2.push_back(temp); else if(temp.d>=temp.c) v3.push_back(temp); else v4.push_back(temp); } } sort(v1.begin(),v1.end()); sort(v2.begin(),v2.end()); sort(v3.begin(),v3.end()); sort(v4.begin(),v4.end()); vector<student>::iterator itr; printf("%d\n",count); for(itr=v1.begin();itr!=v1.end();++itr) printf("%d %d %d\n",itr->num,itr->d,itr->c); for(itr=v2.begin();itr!=v2.end();++itr) printf("%d %d %d\n",itr->num,itr->d,itr->c); for(itr=v3.begin();itr!=v3.end();++itr) printf("%d %d %d\n",itr->num,itr->d,itr->c); for(itr=v4.begin();itr!=v4.end();++itr) printf("%d %d %d\n",itr->num,itr->d,itr->c); system("pause"); return 0;}
C# 实现:
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication1{ class Program { public struct student { public int num; public int d; public int c; public student(int m, int n, int k) { num = m; d = n; c = k; } } static void Main(string[] args) { string str = Console.ReadLine(); int count = int.Parse(str.Split(' ')[0]); int L = int.Parse(str.Split(' ')[1]); int H = int.Parse(str.Split(' ')[2]); List<student> lst1 = new List<student>(); List<student> lst2 = new List<student>(); List<student> lst3 = new List<student>(); List<student> lst4 = new List<student>(); for (int i = 0; i < count; i++) { string[] temp = Console.ReadLine().Split(' '); student s = new student(int.Parse(temp[0]), int.Parse(temp[1]), int.Parse(temp[2])); if (s.d >= L && s.c >= L) { if (s.d >= H && s.c >= H) lst1.Add(s); else if(s.d>=H) lst2.Add(s); else if(s.d>=s.c) lst3.Add(s); else lst4.Add(s); } } lst1.Sort((x, y) => { int value1= (y.d + y.c).CompareTo(x.d + x.c); ; int value2= y.d.CompareTo(x.d); if (value1 != 0) return value1; else if (value2 != 0) return value2; else return x.num.CompareTo(y.num); }); lst2.Sort((x, y) => { int value1 = (y.d + y.c).CompareTo(x.d + x.c); ; int value2 = y.d.CompareTo(x.d); if (value1 != 0) return value1; else if (value2 != 0) return value2; else return x.num.CompareTo(y.num); }); lst3.Sort((x, y) => { int value1 = (y.d + y.c).CompareTo(x.d + x.c); ; int value2 = y.d.CompareTo(x.d); if (value1 != 0) return value1; else if (value2 != 0) return value2; else return x.num.CompareTo(y.num); }); lst4.Sort((x, y) => { int value1 = (y.d + y.c).CompareTo(x.d + x.c); ; int value2 = y.d.CompareTo(x.d); if (value1 != 0) return value1; else if (value2 != 0) return value2; else return x.num.CompareTo(y.num); }); Console.WriteLine(lst1.Count + lst2.Count + lst3.Count + lst4.Count); foreach (student s in lst1) Console.WriteLine("{0} {1} {2}", s.num, s.d, s.c); foreach (student s in lst2) Console.WriteLine("{0} {1} {2}", s.num, s.d, s.c); foreach (student s in lst3) Console.WriteLine("{0} {1} {2}", s.num, s.d, s.c); foreach (student s in lst4) Console.WriteLine("{0} {1} {2}", s.num, s.d, s.c); Console.ReadKey(); } }}
0 0
- 1015. 德才论 (25)
- 1015. 德才论 (25)
- 1015. 德才论 (25)
- 1015. 德才论 (25)
- 1015. 德才论 (25)
- 1015. 德才论 (25)
- 1015. 德才论 (25)
- 1015. 德才论 (25)
- 1015. 德才论 (25)
- 1015. 德才论 (25)
- 1015. 德才论 (25)
- 1015. 德才论 (25)
- 1015. 德才论 (25)
- 1015. 德才论 (25)
- 1015. 德才论 (25)
- 1015. 德才论 (25)
- 1015. 德才论 (25)
- 1015. 德才论 (25)
- 1014. 福尔摩斯的约会 (20)
- SpringMVC
- 最长公共子序列(动态规划方法)和最长递增子序列(动态规划方法)
- 机器学习实践1:基于logistic regression的性别预测
- str库函数模拟实现
- 1015. 德才论 (25)
- Leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal
- Leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal
- 异常:TLD skipped. URI: http://jakarta.apache.org/taglibs/standard/permittedTaglibs is already defined
- 创建线程
- 【Linux】screen 常用命令
- 【Linux】ubuntu系统权限chmod的使用
- expire
- mq