【九度】题目1023:EXCEL排序

题目地址:http://ac.jobdu.com/problem.php?pid=1023
题目描述:
    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
来源:
2007年浙江大学计算机及软件工程研究生机试真题
最典型的排序问题,C++使用sort,Java使用Arrays.sort。
上代码
C++ AC
#include <stdio.h>
#include <algorithm>
#include<string.h>
using namespace std;
const int maxn = 100002; 
struct Student{
    char stuNo[10];
    char name[10];
    int score;
}students[maxn];
int n , c , i;
bool cmp1(Student s1 , Student s2){
    return strcmp(s1.stuNo , s2.stuNo)<0;
} 
bool cmp2(Student s1 , Student s2){
    int tmp = strcmp(s1.name , s2.name) ;
    if(tmp == 0){
        return strcmp(s1.stuNo , s2.stuNo)<0;
    }else {
        return tmp < 0;
    }
} 
bool cmp3(Student s1 , Student s2){
    if(s1.score == s2.score){
        return strcmp(s1.stuNo , s2.stuNo)<0;
    }else {
        return s1.score < s2.score;
    }
} 
int main(){
    int k = 1;
    while(scanf("%d%d",&n,&c) != EOF){
        if(n == 0) break;
        for(i = 0; i < n; i++){
            scanf("%s %s %d",&students[i].stuNo,&students[i].name,&students[i].score);
        }
        if(c==1){
            sort(students, students+n, cmp1);
        }else if(c==2) {
            sort(students, students+n, cmp2);
        }else {
            sort(students, students+n, cmp3);
        }
        printf("Case %d:\n",k);
        for(i = 0; i < n; i++){
            printf("%s %s %d\n",students[i].stuNo,students[i].name,students[i].score);
        }
        k++; 
    }
    return 0;
} 
/**************************************************************
    Problem: 1023
    User: wangzhenqing
    Language: C++
    Result: Accepted
    Time:100 ms
    Memory:3368 kb
****************************************************************/

Java AC

import java.util.Arrays;
import java.util.Scanner;
public class Main { 
    /*
     * 1023
     */
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int count = 1;
        while (scanner.hasNext()) {
            int num = scanner.nextInt();
            Student[] students = new Student[num];
            int seq = scanner.nextInt();
            if (num == 0&&seq==0) {
                break;
            }
            for (int i = 0; i < num; i++) {
                Student student = new Student(seq ,scanner.next(), 
								scanner.next(), scanner.nextInt());
                students[i] = student;
            }
            Arrays.sort(students);
            System.out.println("Case "+count+":");
            for (int i = 0; i < students.length; i++) {
                System.out.println(students[i].getStudentId()+" "+
					students[i].getName()+" "+students[i].getNum());
            }
            count += 1;
        }
        System.out.println();
    }     
}  
class Student implements Comparable<Student>{
    private int caseNo ;
    private String studentId;
    private String name ;
    private int num ;
    public int getCaseNo() {
        return caseNo;
    }
    public void setCaseNo(int caseNo) {
        this.caseNo = caseNo;
    }
    public String getStudentId() {
        return studentId;
    }
    public void setStudentId(String studentId) {
        this.studentId = studentId;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getNum() {
        return num;
    }
    public void setNum(int num) {
        this.num = num;
    }
    public Student(int caseNo, String studentId, 
					String name, int num) {
        super();
        this.caseNo = caseNo;
        this.studentId = studentId;
        this.name = name;
        this.num = num;
    }
    public int compareTo(Student o) {
        if (this.caseNo == 1) {
            return this.getStudentId().compareTo(o.getStudentId());
        }else if (this.caseNo == 2) {
            if (!o.getName().equals(this.getName())) {
                return this.getName().compareTo(o.getName());
            }else {
                return this.getStudentId().compareTo(o.getStudentId());
            }
        }else if (this.caseNo == 3){
            if (o.getNum()!=this.getNum()) {
                return this.getNum()-o.getNum();
            }else {
                return this.getStudentId().compareTo(o.getStudentId());
            }
        }
        return 0;
    }     
} 
/**************************************************************
    Problem: 1023
    User: wzqwsrf
    Language: Java
    Result: Accepted
    Time:2470 ms
    Memory:144608 kb
****************************************************************/




你可能感兴趣的:(【九度】题目1023:EXCEL排序)