- 浏览: 36643 次
- 性别:
- 来自: 杭州
最新评论
PAT1028 List Sorting
- 博客分类:
- PAT
用vector最后一个用例超时了。。。
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
版本一:
#include <stdio.h> #include <stdlib.h> #include <string.h> struct student { char number[7]; char name[9]; short grade; }; int cmp_number(const void *p, const void *q) { const struct student *p1 = (student*)p; const struct student *p2 = (student*)q; return strcmp(p1->number, p2->number); } int cmp_name(const void *p, const void *q) { const struct student *p1 = (student*)p; const struct student *p2 = (student*)q; if(strcmp(p1->name, p2->name) > 0) { return 1; }else if(strcmp(p1->name, p2->name) == 0 && strcmp(p1->number,p2->number) > 0) { return 1; }else { return -1; } } int cmp_grade(const void *p, const void *q) { const struct student *p1 = (student*)p; const struct student *p2 = (student*)q; if(p1->grade > p2->grade) { return 1; }else if(p1->grade == p2->grade && strcmp(p1->number,p2->number) > 0) { return 1; }else { return -1; } } student stus[100002]; int main() { int n, i, type; int count = 1; while(scanf("%d %d", &n, &type) != EOF) { if(n == 0) { break; } for(i = 0; i < n; i ++) { scanf("%s %s %hd",stus[i].number, stus[i].name, &stus[i].grade); } //快速排序 switch(type) { case 1: qsort(stus, n, sizeof(struct student), cmp_number); break; case 2: qsort(stus, n, sizeof(struct student), cmp_name); break; case 3: qsort(stus, n, sizeof(struct student), cmp_grade); break; } for(i = 0; i < n; i ++) { printf("%s %s %hd\n",stus[i].number, stus[i].name, stus[i].grade); } } return 0; }
版本二:
#include <iostream> #include <iomanip> #include <string> #include <vector> #include <algorithm> using namespace std; struct STUDENT { string ID; string Name; int score; }; bool greaterID(const STUDENT& s1,const STUDENT& s2) { return (s1.ID.compare(s2.ID) > 0)?false:true; } bool greaterName(const STUDENT& s1,const STUDENT& s2) { int result = s1.Name.compare(s2.Name); if (result == 0) { return (s1.ID.compare(s2.ID) > 0)?false:true; } return (result > 0)?false:true; } bool greaterScore(const STUDENT& s1,const STUDENT& s2) { if (s1.score == s2.score) { return (s1.ID.compare(s2.ID) > 0)?false:true; } else { return (s1.score > s2.score)?false:true; } } int main() { int N,type; cin>>N>>type; vector<STUDENT> V; for (int i = 0; i < N; i++) { STUDENT stu; cin>>stu.ID>>stu.Name>>stu.score; V.push_back(stu); } switch(type) { case 1: sort(V.begin(),V.end(),greaterID); break; case 2: sort(V.begin(),V.end(),greaterName); break; case 3: sort(V.begin(),V.end(),greaterScore); break; default: break; } for (int i = 0; i < N; i++) { cout<<V[i].ID<<" "<<V[i].Name<<" "<<V[i].score<<endl; } }
发表评论
-
PAT1013 Battle Over Cities
2012-11-29 23:59 773Sample Input 3 2 3 1 2 1 3 ... -
PAT1003 Emergency
2012-11-29 23:46 661Sample Input 5 6 0 2 1 2 1 ... -
PAT1041 Be Unique
2012-11-23 23:43 760找出只出现过一次的数,用各种排序必然超时,需要用数组做hash ... -
PAT1042 Shuffling Machine
2012-11-23 23:42 733扑克洗牌 #include < ... -
PAT1040 Longest Symmetric String
2012-11-23 23:41 959求最长回文子串 #include < ... -
PAT1036 Boys vs Girls
2012-11-23 23:41 716Sample Input 1: 3 Joe M Mat ... -
PAT1035 Password
2012-11-23 23:40 616Sample Input 1: 3 Team0000 ... -
PAT1031 Hello World for U
2012-11-22 23:54 655Sample Input: helloworld! S ... -
PAT1029 Median
2012-11-22 23:54 651用标准库的排序全部超时,需要自己实现,另外还不能用cin co ... -
PAT1027 Colors in Mars
2012-11-22 23:52 623Sample Input 15 43 71 Samp ... -
PAT1025 PAT Ranking
2012-11-22 23:51 771Sample Input: 2 5 123456789 ... -
PAT1023 Have Fun with Numbers
2012-11-21 23:55 687大数的相加 比较两个字符串中字符完全相同 Sa ... -
PAT1020 Tree Traversals
2012-11-21 23:54 652已知中序遍历 后序遍历,求层次遍历 Sample In ... -
PAT1019 General Palindromic Number
2012-11-21 23:53 537十进制转任意进制,并比较是否是回文数 Sample I ... -
PAT1037 Magic Coupon
2012-11-21 15:46 663Sample Input: 4 1 2 4 -1 ... -
PAT1038 Recover the Smallest Number
2012-11-20 23:52 1668由一道面试题改的 把数组排成最小的数 不同之处是这 ... -
PAT1024 Palindromic Number
2012-11-20 23:51 648Sample Input 1: 67 3 Sampl ... -
PAT1015 Reversible Primes
2012-11-19 23:51 771十进制转任意进制 假设十进制数为number,转 ... -
PAT1012 The Best Rank
2012-11-19 23:50 926四门功课,输出排名最高的是哪个 Sample Inpu ... -
PAT1011 World Cup Betting
2012-11-19 23:50 534Sample Input 1.1 2.5 1.7 1.2 ...
相关推荐
javascript、sorting、algorithm
DNA Sorting Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 34868 Accepted: 13480 Description One measure of ``unsortedness'' in a sequence is the number of pairs of entries that are ...
快速排序sorting
Java实现常用sorting算法,包括insertion, merge, bubble, heap, quick, couting, radix, bucket and maxHeap/Priority queue sorting。并对算法复杂度使用场景做了分析。 主要参考资料wikipedia, CLRS算法教材
随机数列,可控制上下界线。冒泡算法,c++,bubble sorting
为NGUI添加sortting layer,修改了3个文件,放在这里,覆盖掉以前的文件即可。 注意版本,我这个是NGUI 3.6.8基础上修改的。详细修改过程见我的博客文章。
北大POJ1094-Sorting It All Out 解题报告+AC代码
生成一个可以控制上下界限的随机数列,并使用选择排序selection sorting算法对其排序。
Multi-objective evolutionary algorithms which use non-dominated sorting and sharing have been mainly criticized for their (i) computational complexity (where is the number of objectives and...
poj dna sorting 问题,研究的ac coderrrrrrr
找了很久的外排序,不过现在很多都用数据库代替了,对提高代码功力还是有用的.
Algorithm-sorting-visualization.zip,生成gif的命令行工具,可以显示排序算法,算法是为计算机程序高效、彻底地完成任务而创建的一组详细的准则。
10-1 Sorting Concepts 10-2 Insertion Sorts 10-2-1 Straight Insertion Sort 10-2-2 Shell Sort 10-3 Selection Sorts 10-3-1 Straight Selection Sort 10-3-2 Heap Sort 10-4 Exchange Sorts 10-4-1 Bubble Sort ...
The Art of Computer Programming Volume 3 Sorting and Searching, 2nd Edition
Sorting and Searching Algorithms:A Cookbook 常用算法数据结构
An Adaptive Generic Sorting Algorithm that Uses Variable Partitioning。pdf.pdfAn Adaptive Generic Sorting Algorithm that Uses Variable Partitioning。pdf.pdfAn Adaptive Generic Sorting Algorithm that ...
all kinds of sortings
Algorithm-sorting-algorithms-performance-comparison.zip,一组排序算法的性能比较它计算每个算法用于排序列表的时间:已排序的数字和未排序的数字,算法是为计算机程序高效、彻底地完成任务而创建的一组详细的准则...
pku acm 1007 DNA Sorting代码 逆序数 排序 解题报告请访问:http://blog.csdn.net/china8848