# Jolly Jumpers(UVA10038) ## [程式繳交區](https://hackmd.io/@Renektonn/BJsTO_0Dkl/edit) ## 題目 [點我](https://onlinejudge.org/external/100/10038.pdf) ## 解題網站 [UVA](https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=12&page=show_problem&problem=979) [ZJ](https://zerojudge.tw/ShowProblem?problemid=d097) ## 演算法 ``` 1.輸入n,若無法輸入,則停止 2.若n小於等於0,則輸出Not jolly,並且continue 3.宣告布林變數flag=1,再宣告陣列cnt,初始值為0,長度為n,再宣告陣列arr 4.for i =1 to n:輸入arr i 5.for i=2 to n: 5.1.若abs(arr[i]-arr[i-1])不在1~n-1,或cnt[abs(arr[i]-arr[i-1])]不為0,則輸出Not jolly,並將flag設為0 5.2.更新cnt 6.若flag,則輸出Jolly 7.回到1 ``` ## 程式碼 ```cpp= #include <iostream> #include <vector> #include <cmath> using namespace std; int main() { int n; while (cin >> n) { // 持續讀入 n,直到無法輸入 if (n <= 0) { // 若 n 小於等於 0 cout << "Not jolly" << endl; continue; } bool flag = true; // 初始化 flag 為 true vector<int> cnt(n, 0); // 宣告陣列 cnt,初始值為 0,長度為 n vector<int> arr(n); // 宣告陣列 arr,長度為 n // 讀入陣列 arr for (int i = 0; i < n; ++i) { cin >> arr[i]; } // 檢查差值是否符合條件 for (int i = 1; i < n; ++i) { int diff = abs(arr[i] - arr[i - 1]); if (diff < 1 || diff >= n || cnt[diff] != 0) { // 不在範圍內或重複 cout << "Not jolly" << endl; flag = false; break; } cnt[diff] = 1; // 標記該差值 } if (flag) { cout << "Jolly" << endl; // 若符合條件,輸出 Jolly } } return 0; } ```