本文主要是介绍【九度】题目1187:最小年龄的3个职工,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
- 题目地址:http://ac.jobdu.com/problem.php?pid=1187
-
职工有职工号,姓名,年龄.输入n个职工的信息,找出3个年龄最小的职工打印出来。
题目描述:
- 输入:
-
输入第一行包括1个整数N,1<=N<=30,代表输入数据的个数。
接下来的N行有N个职工的信息:
包括职工号(整数), 姓名(字符串,长度不超过10), 年龄(1<=age<=100)。
- 输出:
-
可能有多组测试数据,对于每组数据,
输出结果行数为N和3的较小值,分别为年龄最小的职工的信息。关键字顺序:年龄>工号>姓名,从小到大。
- 样例输入:
-
5 501 Jack 6 102 Nathon 100 599 Lily 79 923 Lucy 15 814 Mickle 65
- 样例输出:
-
501 Jack 6 923 Lucy 15 814 Mickle 65
- 来源:
- 2003-2005年华中科技大学计算机研究生机试真题
-
再熟悉不过的排序了。
除了使用stl自带的sort函数以外,可以自己尝试写一写快速排序。
恩,我自己写了一个。
C++ AC -
#include <stdio.h> #include <algorithm> #include<string.h> using namespace std; const int maxn = 32; struct Member{int id;char name[102];int age; }members[maxn]; int n , i; bool cmp(Member m1 , Member m2){if(m1.age != m2.age){return m1.age < m2.age;}else {if(m1.id != m2.id){return m1.id < m2.id;}else{return strcmp(m1.name ,m2.name) < 0;}} } int sortUnit(int low ,int high){Member m;m.id = members[low].id;strcpy(m.name,members[low].name);m.age = members[low].age;while(low < high){while(low < high && cmp(m,members[high])){high--;}members[low].id = members[high].id;strcpy(members[low].name,members[high].name);members[low].age = members[high].age;while(low < high && cmp(members[low],m)){low++;}members[high].id = members[low].id;strcpy(members[high].name,members[low].name);members[high].age = members[low].age;}members[high].id = m.id;strcpy(members[high].name,m.name);members[high].age = m.age;return high; } void sort(int low ,int high){if(low > high){return;}int index = sortUnit(low,high);sort(low ,index-1);sort(index+1,high); } int main(){while(scanf("%d",&n) != EOF){for(i = 0; i < n; i++){scanf("%d %s %d",&members[i].id,&members[i].name,&members[i].age);}sort(0,n-1);int tmp = n < 3 ? n : 3;for(i = 0; i < tmp; i++){printf("%d %s %d\n",members[i].id,members[i].name,members[i].age);}}return 0; } /**************************************************************Problem: 1187User: wangzhenqingLanguage: C++Result: AcceptedTime:10 msMemory:1024 kb ****************************************************************/
Java AC 输入改StreamTokenizer估计会快许多。
-
import java.util.Arrays; import java.util.Scanner;public class Main {/** 1187*/public static void main(String[] args) throws Exception {Scanner scanner = new Scanner(System.in);while (scanner.hasNext()) {int N = scanner.nextInt();Staff[] staffArray = new Staff[N];for (int i = 0; i < N; i++) {int staffId = scanner.nextInt();String name = scanner.next();int age = scanner.nextInt();Staff staff = new Staff(staffId, name, age);staffArray[i] = staff;}Arrays.sort(staffArray);if ( N < 3) {for (int i = 0; i < N; i++) {System.out.println(staffArray[i].getStaffId()+" "+staffArray[i].getStaffName()+" "+staffArray[i].getAge());}}else {for (int i = 0; i < 3; i++) {System.out.println(staffArray[i].getStaffId()+" "+staffArray[i].getStaffName()+" "+staffArray[i].getAge());}}}} } class Staff implements Comparable<Staff>{private int staffId;private String staffName ;private int age;public int getStaffId() {return staffId;}public void setStaffId(int staffId) {this.staffId = staffId;}public String getStaffName() {return staffName;}public void setStaffName(String staffName) {this.staffName = staffName;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public Staff(int staffId, String staffName, int age) {super();this.staffId = staffId;this.staffName = staffName;this.age = age;}public int compareTo(Staff o) {if (this.getAge()!= o.getAge()) {return this.getAge()-o.getAge();}else {if (this.getStaffId()!= o.getStaffId()) {return this.getStaffId()- o.getStaffId();}else {return this.getStaffName().compareTo(o.getStaffName());}}} } /**************************************************************Problem: 1187User: wzqwsrfLanguage: JavaResult: AcceptedTime:480 msMemory:44708 kb ****************************************************************/
这篇关于【九度】题目1187:最小年龄的3个职工的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!