码迷,mamicode.com
首页 > 编程语言 > 详细

ACM&排序问题,操作符重载

时间:2014-10-17 23:15:05      阅读:391      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   ar   使用   for   

题目描述:
    Excel可以对一组纪录按任意指定列排序。现请你编写程序实现类似功能。
    对每个测试用例,首先输出1行“Case i:”,其中 i 是测试用例的编号(从1开始)。随后在 N 行中输出按要求排序后的结果,即:当 C=1 时,按学号递增排序;当 C=2时,按姓名的非递减字典序排序;当 C=3 
时,按成绩的非递减排序。当若干学生具有相同姓名或者相同成绩时,则按他们的学号递增排序。
输入:

    测试输入包含若干测试用例。每个测试用例的第1行包含两个整数 N (N<=100000) 和 C,其中 N 是纪录的条数,C 是指定排序的列号。以下有N行,每行包含一条学生纪录。每条学生纪录由学号(6位数字,同组测试中没有重复的学号)、姓名(不超过8位且不包含空格的字符串)、成绩(闭区间[0, 100]内的整数)组成,每个项目间用1个空格隔开。当读到 N=0 时,全部输入结束,相应的结果不要输出。

输出:
    对每个测试用例,首先输出1行“Case i:”,其中 i 是测试用例的编号(从1开始)。随后在 N 行中输出按要求排序后的结果,即:当 C=1 时,按学号递增排序;当 C=2时,按姓名的非递减字典序排序;当 C=3 
时,按成绩的非递减排序。当若干学生具有相同姓名或者相同成绩时,则按他们的学号递增排序。
样例输入:
3 1
000007 James 85
000010 Amy 90
000001 Zoe 60
4 2
000007 James 85
000010 Amy 90
000001 Zoe 60
000002 James 98
4 3
000007 James 85
000010 Amy 90
000001 Zoe 60
000002 James 90
0 0
样例输出:
Case 1:
000001 Zoe 60
000007 James 85
000010 Amy 90
Case 2:
000010 Amy 90
000002 James 98
000007 James 85
000001 Zoe 60
Case 3:
000001 Zoe 60
000007 James 85
000002 James 90
000010 Amy 90

http://ac.jobdu.com/problem.php?pid=1023
bubuko.com,布布扣
 1 #include <iostream>
 2 #include <string>
 3 #include <algorithm>
 4 #define NUMSIZE 100010
 5 
 6 using namespace std;
 7 int Mode;
 8 class Stu_index {
 9     public:
10         string num;
11         string name;
12         int score;
13         Stu_index(){
14         }
15         Stu_index(string num,string name, int score) {
16             this->num = num;
17             this->name = name;
18             this->score = score;
19         }
20         friend bool operator < (const Stu_index& a, const Stu_index& b) {
21             if(Mode == 1)
22                 return a.num<b.num;
23             if(Mode == 2)
24                 if(a.name!=b.name)  return a.name<b.name;
25                 else return a.num<b.num;
26             if(Mode == 3)
27                 if(a.score!=b.score)    return a.score<b.score;
28                 else return a.num<b.num;
29         }
30         friend bool operator > (const Stu_index& a, const Stu_index& b) {
31             if(Mode == 1)
32                 return a.num>b.num;
33             if(Mode == 2)
34                 if(a.name!=b.name)  return a.name>b.name;
35                 else return a.num>b.num;
36             if(Mode == 3)
37                 if(a.score!=b.score)    return a.score>b.score;
38                 else return a.num>b.num;
39         }
40         friend ostream &operator<< (ostream &os, const Stu_index &item) {
41             os<<item.num<<" "<<item.name<<" "<<item.score;
42             return os;
43         }
44 };
45 /*void quicksort(Stu_index v[], int left, int right) {
46     if(left<right) {
47         Stu_index key = v[left];
48         int low = left;
49         int high = right;
50         while(low<high) {
51             while(low<high && v[high]>key) high--;
52             v[low] = v[high];
53             while(low<high && v[low]<key) low++;
54             v[high] = v[low];
55         }
56         v[low] = key;
57         quicksort(v, left, low-1);
58         quicksort(v, low+1, right);
59     }
60 }*/
61 int main(void) {
62     int N, C;
63     C=0;
64     Stu_index s[NUMSIZE];
65     while(cin>>N && N!=0) {
66         C++;
67         cin>>Mode;
68         for(int i=0; i<N; i++) {
69             cin>>s[i].num>>s[i].name>>s[i].score;
70         }
71         //quicksort(s, 0, N-1);
72         sort(s, s+N);
73         cout<<"Case "<<C<<":"<<endl;
74         for(int i=0; i<N; i++)
75             cout<<s[i]<<endl;
76     }
77     return 0;
78 }
View Code

这里使用了重载操作符,但类中的静态变量在没有初始化的时候类中的方法是无法使用改变量的,于是我将Mode改成了全局变量。


ACM&排序问题,操作符重载

标签:style   blog   http   color   io   os   ar   使用   for   

原文地址:http://www.cnblogs.com/wizzhangquan/p/4032156.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!