---
# System prepended metadata

title: Java_Cmoney_ft7206
tags: [Cmoney_Java題目]

---

---
tags: Cmoney_Java題目
---
Java_Cmoney_ft7206
===

![](https://i.imgur.com/lEwNhJ4.png)
![](https://i.imgur.com/pQQ4mNf.png)
![](https://i.imgur.com/6KW8G6C.png)






1.需要的 function
---
1.1 是否是等差數列
---

```java=
public static boolean f(int[] arr) {
        int r = arr[1] - arr[0];
        for (int i = 0; i < arr.length - 1; i++) {
            if (r != arr[i + 1] - arr[i])
                return false;
        }
        return true;
    }
```



2.主程式
---
1. 創造兩個陣列，一個是輸入的數列，一個是這個數列的"差"數列
2. 使用上面的 function，看 arr1 是不是等差數列 

```java=
public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] arr = new int[n];
        int[] arr1 = new int[n - 1];
        for (int i = 0; i < n; i++)
            arr[i] = sc.nextInt();
        for (int i = 0; i < n - 1; i++)
            arr1[i] = arr[i + 1] - arr[i];
        boolean anw = f(arr1);
        System.out.println(anw);

    }
```

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();
        int[] arr = new int[n];
        int[] arr1 = new int[n - 1];
        for (int i = 0; i < n; i++)
            arr[i] = sc.nextInt();
        for (int i = 0; i < n - 1; i++)
            arr1[i] = arr[i + 1] - arr[i];
        boolean anw = f(arr1);
        System.out.println(anw);

    }

    public static boolean f(int[] arr) {
        int r = arr[1] - arr[0];
        for (int i = 0; i < arr.length - 1; i++) {
            if (r != arr[i + 1] - arr[i])
                return false;
        }
        return true;
    }
}
```