定义一个学生结构体类型student,包括4个字段,姓名、性别、年龄和成绩。然后在主函数中定义一个结构体数组(长度不超过1000),
并输入每个元素的值,程序使用冒泡排序法将学生按照成绩从小到大的顺序排序,然后输出排序的结果。
  输入格式:第一行是一个整数N(N<1000),表示元素个数;接下来N行每行描述一个元素,姓名、性别都是长度不超过20的字符串,年龄和
成绩都是整型。
  输出格式:按成绩从小到大输出所有元素,若多个学生成绩相同则成绩相同的同学之间保留原来的输入顺序。
输入:
  3
  Alice female 18 98
  Bob male 19 90
  Miller male 17 92

输出:
  Bob male 19 90
  Miller male 17 92
  Alice female 18 98

Java实现 蓝桥杯VIP 算法训练 P1102_java

import java.util.Scanner;



class Student{//这里建一个类只是为了方便一些,
	//其实建四个数组也是可以的
    String name;
    String gender;
    int age;
    int credit;
}
public class P1102 {
	 public static void main(String[] args) {
	        Scanner sc = new Scanner(System.in);
	        int num = sc.nextInt();
	        Student[] arr = new Student[num];
	        for(int i = 0 ;i < arr.length;i ++) {
	            arr[i] = new Student();
	            arr[i].name = sc.next();
	            arr[i].gender = sc.next();
	            arr[i].age = sc.nextInt();
	            arr[i].credit = sc.nextInt();
	        }
	        int j ;
	        Student target = new Student();
	        for(int i = 0 ;i < arr.length;i ++) {
	            j = i;
	            target = arr[i];
	            while (j > 0 && target.credit < arr[j - 1].credit){
	                arr[j] = arr[j - 1];
	                j--;
	            }
	            arr[j] = target;
	        }
	        for(int i = 0;i < arr.length;i ++) {
	            System.out.println(arr[i].name + " " + arr[i].gender + " " + arr[i].age + " " + arr[i].credit);
	        }
	    }

}

import java.util.Arrays;
import java.util.Scanner;

public class P1102 {
	public static class People implements Comparable<People>{
		int id;
		String name;
		int score;
		public People(String a,String b,String c,int score,int id){
			this.name=a+" "+b+" "+c;
			this.id=id;
			this.score=score;
		}
		@Override
		public int compareTo(People o) {
			if(this.score==o.score){
				return this.id-o.id;
			}
			return this.score-o.score;
			 
		}
		public String toString(){
			 return this.name+" "+this.score;
			
		}
	}
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		People[] num = new People[n];
		for (int i = 0; i < num.length; i++) {
			String a = sc.next();
			String b = sc.next();
			String c = sc.next();
			int score =sc.nextInt();
			num[i] = new People(a,b,c,score,i);
		}
		Arrays.sort(num);
		for (int i = 0; i < num.length; i++) {
			System.out.println(num[i].toString());
		}
		
		
		
	}

}