输入描述:
输入第一行包括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
JAVA实现代码:
(一)
import java.util.*;
class Worker{
private int age;
private String name;
private int workerID;
public Worker(int workerID,String name,int age){
this.workerID=workerID;
this.name=name;
this.age=age;
}
public void setWorkerID(int workerID){
this.workerID=workerID;
}
public int getWorkerID(){
return workerID;
}
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
public void setAge(int age){
this.age=age;
}
public int getAge(){
return age;
}
}
public class Main{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
while(in.hasNext())
{
int N=Integer.parseInt(in.nextLine());
Worker []worker=new Worker[N];
for(int i=0;i<N;i++)
{
String [] str=in.nextLine().split(" ");
worker[i]=new Worker(Integer.parseInt(str[0]),str[1],Integer.parseInt(str[2]));
}
for(int j=0;j<N;j++)
{
for(int k=j+1;k<N;k++)
{
if(worker[j].getAge()>worker[k].getAge())
{
Worker temp=worker[j];
worker[j]=worker[k];
worker[k]=temp;
}
}
}
for(int p=0;p<3;p++)
{
System.out.println(worker[p].getWorkerID()+" "+worker[p].getName()+" "+worker[p].getAge());
}
}
}
}
(二)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = null;
while ((line = br.readLine()) != null) {
int N = Integer.parseInt(line);
String[] info = new String[N];
for (int i = 0; i < N; i++) {
info[i] = br.readLine();
}
SortPrint(info, N, Math.min(N, 3));
}
}
private static void SortPrint(String[] info, int n, int count) {
int[] age = new int[n];
for (int i = 0; i < n; i++) {
String[] split = info[i].split(" ");
age[i] = Integer.parseInt(split[split.length - 1]);
}
while (count > 0) {
int index = 0;
for (int i = 1; i < n; i++) {
if (age[index] > age[i] && age[i] < 101 ){
index = i;
}else if (age[index] == age[i] ) {
int no1 = Integer.parseInt(info[index].split(" ")[0]);
int no2 = Integer.parseInt(info[i].split(" ")[0]);
if (no1 > no2) {
index = i;
}
}
}
age[index] = 101;
System.out.println(info[index]);
count--;
}
}
}
(三)
class Info implements Comparable<Info>{
public int age;
public int number;
public String name;
public Info(int number, String name, int age){
this.age = age;
this.number = number;
this.name = name;
}
@Override
public int compareTo(Info o){
if(this.age!= o.age){
return this.age-o.age;
}else{
return this.number-o.number;
}
}
}
(四)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* 职工有职工号,姓名,年龄.输入n个职工的信息,找出3个年龄最小的职工打印出来。
*/
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = null;
// 可能有多组测试数据
while ((line = br.readLine()) != null) {
//输入第一行包括1个整数N,1<=N<=30,代表输入数据的个数。
int n = Integer.parseInt(line);
String[][] data = new String[n][3];
for (int i = 0; i < n; i++) {
data[i] = br.readLine().split(" ");
}
sort(data,n);
for (int i = 0; i < 3; i++) {
System.out.print(data[i][0] + " ");
System.out.print(data[i][1] + " ");
System.out.println(data[i][2]);
}
}
}
private static void sort(String[][] data,int n) {
//按年龄排序 选择排序
String[] temp = new String[3];
for (int i = 0; i < 3 ; i++) {
for (int j = i + 1; j < n; j++) {
if( data[i][2].length() > data[j][2].length()
|| (data[i][2].length() == data[j][2].length() && data[i][2].compareTo(data[j][2]) > 0 )) {
temp = data[i];
data[i] = data[j];
data[j] = temp;
}
}
}
}