---
# System prepended metadata

title: Java_Cmoney_ft7107
tags: [Cmoney_Java題目]

---

---
tags: Cmoney_Java題目
---
Java_Cmoney_ft7107
===


![](https://i.imgur.com/HCD1aFP.png)
![](https://i.imgur.com/O7ZRbTc.png)



1.需要的 function
---
1.1 方法一 遞迴+小數無條件進位
---
1. 使用遞迴回傳來計算不同小時可以騎幾公里，然後加在一起
2. 總距離/一天可以騎幾公里 = 要花幾天(有小數點)
3. `(int) Math.ceil(要花幾天(float))`，無條件進位，再轉成 int
```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 K = sc.nextInt();
        int M = sc.nextInt();
        int sum = 0;
        for (int i = 1; i <= K; i++) {
            sum += F(N, i);
        }
        float re = (float) M / sum;
        System.out.println((int) Math.ceil(re));
    }

    public static int F(int N, int K) {
        if (K == 1)
            return N;
        else
            return F(N, K - 1) * 90 / 100;
    }
}
```


1.2 方法二 純迴圈
---
1. 原本遞迴的地方，改成用迴圈
2. 特別注意，不同小時騎幾公里、一天騎幾公里、總共要騎幾公里，
    三個變數要分開
3. 最後一個while迴圈，是代替用除的算要騎幾天 
```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 K = sc.nextInt();
        int M = sc.nextInt();
        int dayRide = N;
        int sum = 0;
        for (int i = 0; i < K - 1; i++) {
            N = N * 90 / 100;
            dayRide += N;
        }
        int count = 0;
        while (sum <= M) {
            sum += dayRide;
            count++;
        }
        System.out.println(count);
    }
}
```


1.3 方法三 遞迴+迴圈
---
1. 改良第一版遞迴，可以計算一天可以騎多少公里
2. 使用迴圈計算何時會超過目標距離
```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 k = sc.nextInt();
        int m = sc.nextInt();
        int d = 0;
        int ride = 0;
        while (ride <= m) {
            ride += ride(n, k);
            d++;
        }
        System.out.println(d);
    }

    public static int ride(int n, int k) {
        if (k == 1)
            return n;
        return ride(n, k - 1) + n - n * 10 / 100 * (k - 1);
    }
}
```