---
tags: Cmoney_Java題目
---
Java_Cmoney_st206
===


1.需要的 function 和 Class
---
1.1 Class Stu
---
1. attribute : seat, score
2. method : Stu(), print()
```java=
class Stu {
int seat;
int score ;
Stu(int seat, int score) {
this.seat = seat;
this.score = score;
}
void print() {
System.out.println(this.seat + "." + this.score);
}
}
```
1.2 交換Stu陣列裡面的Stu物件
---
```java=
public static void swap(Stu[] stus, int i1, int i2) {
Stu tmp = stus[i1];
stus[i1] = stus[i2];
stus[i2] = tmp;
}
```
1.3 泡沫排序法
---
```java=
public static void sortScore(Stu[] stus) {
for (int i = 0; i < stus.length - 1; i++) {
for (int j = 0; j < stus.length - i - 1; j++) {
if (stus[j + 1].score > stus[j].score)
swap(stus, j, j + 1);
}
}
}
```
2.主程式
---
```java=
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
Stu[] stus = new Stu[n];
for (int i = 0; i < stus.length; i++) {
stus[i] = new Stu(sc.nextInt(), sc.nextInt());
}
sortScore(stus);
for (int i = 0; i < stus.length; i++) {
stus[i].print();
}
}
```
3.完整程式
---
```java=
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
Stu[] stus = new Stu[n];
for (int i = 0; i < stus.length; i++) {
stus[i] = new Stu(sc.nextInt(), sc.nextInt());
}
sortScore(stus);
for (int i = 0; i < stus.length; i++) {
stus[i].print();
}
}
public static void sortScore(Stu[] stus) {
for (int i = 0; i < stus.length - 1; i++) {
for (int j = 0; j < stus.length - i - 1; j++) {
if (stus[j + 1].score > stus[j].score)
swap(stus, j, j + 1);
}
}
}
public static void swap(Stu[] stus, int i1, int i2) {
Stu tmp = stus[i1];
stus[i1] = stus[i2];
stus[i2] = tmp;
}
}
class Stu {
int seat;
int score ;
Stu(int seat, int score) {
this.seat = seat;
this.score = score;
}
void print() {
System.out.println(this.seat + "." + this.score);
}
}
```