定义一个学生结构体类型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

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());
} } }

最新文章

  1. Sicily 1051: 魔板(BFS+排重)
  2. 在iis7上如何配置来看到asp报错
  3. winform中button的image属性无法更改
  4. ES6新特性之模板字符串
  5. Auty自动化测试框架第三篇——添加异常处理与日志收集
  6. springmvc:frameServletBean
  7. codeforces 678D Iterated Linear Function 矩阵快速幂
  8. python引用在函数传参时的体现以及可变与不可变对象的对比
  9. C语言的本质(23)——C标准库之输入与输出(上)
  10. 基于visual Studio2013解决算法导论之015第二小元素
  11. java--多线程之Thread继承
  12. IDEA 单元测试testng入门及testng.xml
  13. 练习使用markdown
  14. Python装饰器小代码
  15. 单选、多选框根据value值设置选中
  16. nginx配置二级目录,反向代理不同ip+端口
  17. 外部程序调用Django模块的解决办法
  18. oracle如何通过cmd导出某个用户下的所有表
  19. linux test
  20. python 核心编程 01

热门文章

  1. 存储过程——公用表表达式(CTE)
  2. 1.Spring 框架概述
  3. 【图论算法】LCA最近公共祖先问题
  4. 我,不是说了PID要平均值吗?
  5. Qt版本中国象棋开发(四)
  6. PAT 1032 Sharing (25分) 从自信到自闭
  7. [批处理教程之Shell]002.Linux 常用命令大全
  8. 【转】从一副扑克牌中随机抽取N张
  9. java方式实现归并排序
  10. 基于RBAC的权限控制浅析(结合Spring Security)