---
# System prepended metadata

title: Java_Cmoney_st108
tags: [Cmoney_Java題目]

---

---
tags: Cmoney_Java題目
---
Java_Cmoney_st108
===

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




1.需要的 function
---
1.1 計算攀爬距離函數
---
攀爬的數字是有小數的，所以使用 Doubl。day 記得減1，因為第一天不會疲勞，會爬好爬滿。

```java=
public static double climb(double k, int f, int day) {
        return k - k * f / 100 * (day - 1);
    }
```



2.主程式
---
這裡要注意，如果攀爬的距離超過目標 x ，就可以跳出 while 迴圈，不用再等到晚上下滑 L，所以
1. 先爬    `climb += climb(k, f, day)`
2. 判斷有沒有超過目標高度 X
3. 再晚上滑落 L
4. 判斷有沒有掉到地上(變負值)
    

```java=
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        for (int i = 0; i < n; i++) {
            int k = sc.nextInt();
            int l = sc.nextInt();
            int x = sc.nextInt();
            int f = sc.nextInt();
            double climb = 0;
            int day = 1;
            while (true) {
                climb += climb(k, f, day);
                if (climb > x) {
                    System.out.println("success-" + day);
                    break;
                }
                climb -= l;
                if (climb < 0) {
                    System.out.println("fail-" + day);
                    break;
                }
                day++;
            }
        }
    }
```

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();
        for (int i = 0; i < n; i++) {
            int k = sc.nextInt();
            int l = sc.nextInt();
            int x = sc.nextInt();
            int f = sc.nextInt();
            double climb = 0;
            int day = 1;
            while (true) {
                climb += climb(k, f, day);
                if (climb > x) {
                    System.out.println("success-" + day);
                    break;
                }
                climb -= l;
                if (climb < 0) {
                    System.out.println("fail-" + day);
                    break;
                }
                day++;
            }
        }
    }

    public static double climb(double k, int f, int day) {
        return k - k * f / 100 * (day - 1);
    }
}
```