---
# System prepended metadata

title: Java_Cmoney_ft8108
tags: [Cmoney_Java題目]

---

---
tags: Cmoney_Java題目
---
Java_Cmoney_ft8108
===

![](https://i.imgur.com/9d2OlkS.png)
![](https://i.imgur.com/pwsY08Q.png)



1.需要的 function 和 Class
---
1.1 玩家 Class 
---
1. attributes : seat, win, score
2. print()

```java=
class Dice {
    int seat;
    int win;
    int score;

    Dice(int seat, int win, int score) {
        this.seat = seat;
        this.win = win;
        this.score = score;
    }

    void print() {
        System.out.println(this.win);
    }
}
```


1.2 交換 function
---

```java=
public static void swap(Dice[] dices, int i1, int i2) {
        Dice tmp = dices[i1];
        dices[i1] = dices[i2];
        dices[i2] = tmp;
    }
```


1.3 分數排序(泡沫排序法)
---
外層迴圈代表要幾個泡泡才能排完(n - 1)，
內層代表每一個泡泡要比幾次大小才能排完(n - 1)，
可是每當泡泡排好就不用管他了，所以第 i 圈就可以少交換 i次，
因為有 i 個泡泡被固定了，所以(n - i - 1)。

```java=
public static void sortScore(Dice[] dices) {
        for (int i = 0; i < dices.length - 1; i++)
            for (int j = 0; j < dices.length - i - 1; j++) {
                if (dices[j].score > dices[j + 1].score)
                    swap(dices, j, j + 1);
            }
    }
```


1.4 座號排序(泡沫排序法)
---
```java=
public static void sortSeat(Dice[] dices) {
        for (int i = 0; i < dices.length - 1; i++)
            for (int j = 0; j < dices.length - i - 1; j++) {
                if (dices[j].seat > dices[j + 1].seat )
                    swap(dices, j, j + 1);
            }
    }
```

2.主程式
---
1. 先建立有幾個玩家類別陣列，再用迴圈建構子
2. 要玩幾局的迴圈
    1. 迴圈輸入玩家分數
    2. 以分數排序
    3. 如果分數和最後一個一樣(代表最大)，win++
    4. ==以位置排序，這樣陣列裡的順序才會對，
       下次加分數進去，才不會加錯人，重要!!!!!!!!!!!!!!!!!!!==
3. 最後將結果印出

```java=
public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Dice[] dices = new Dice[sc.nextInt()];
        
        for (int i = 0; i < dices.length; i++) {
            dices[i] = new Dice(i, 0, 0);
        }

        int n = sc.nextInt();
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < dices.length; j++) {
                dices[j].score = sc.nextInt();
            }
            sortScore(dices);
            for (int j = 0; j < dices.length; j++) {
                if (dices[j].score == dices[dices.length - 1].score)
                    dices[j].win++;
            }
            sortSeat(dices);
        }
        for (int j = 0; j < dices.length; j++) {
            dices[j].print();
        }
    }
```

3.完整程式
---
```java=
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Dice[] dices = new Dice[sc.nextInt()];
      
        for (int i = 0; i < dices.length; i++) {
            dices[i] = new Dice(i, 0, 0);
        }

        int n = sc.nextInt();
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < dices.length; j++) {
                dices[j].score = sc.nextInt();
            }
            sortScore(dices);
            for (int j = 0; j < dices.length; j++) {
                if (dices[j].score == dices[dices.length - 1].score)
                    dices[j].win++;
            }
            sortSeat(dices);
        }
        for (int j = 0; j < dices.length; j++) {
            dices[j].print();
        }
    }

    public static void sortScore(Dice[] dices) {
        for (int i = 0; i < dices.length - 1; i++)
            for (int j = 0; j < dices.length - i - 1; j++) {
                if (dices[j].score > dices[j + 1].score)
                    swap(dices, j, j + 1);
            }
    }

    public static void sortSeat(Dice[] dices) {
        for (int i = 0; i < dices.length - 1; i++)
            for (int j = 0; j < dices.length - i - 1; j++) {
                if (dices[j].seat > dices[j + 1].seat )
                    swap(dices, j, j + 1);
            }
    }

    public static void swap(Dice[] dices, int i1, int i2) {
        Dice tmp = dices[i1];
        dices[i1] = dices[i2];
        dices[i2] = tmp;
    }
}

class Dice {
    int seat;
    int win;
    int score;

    Dice(int seat, int win, int score) {
        this.seat = seat;
        this.win = win;
        this.score = score;
    }

    void print() {
        System.out.println(this.win);
    }
}
```