Linear Search (Function)


Submit solution

Points: 50
Time limit: 1.0s
Memory limit: 10M

Authors:
Problem type
Allowed languages
Java 19

Description

請設計一個 Java 程式,在主程式 main 中建立以下已排序的字元陣列:{'a', 'c', 'm', 'n', 'p', 'q', 's', 't', 'v', 'w', 'x', 'y', 'z'} 程式需提示使用者輸入一個字元,並使用 Linear Search 且必須使用 method 實作 進行搜尋。


Requirements

  1. 請先在 main 外部設定一 class 叫做 SearchUtils
  2. SearchUtils 實作一個 Linear Search 的 method,例如:linearSearch(char[] arr, char target)
  3. 搜尋過程中需計算搜尋次數 ,且每進入一次比較算一次 ++
  4. 若找到該字元,輸出:
    • 該字元在陣列中的 index (從 0 開始)
    • 搜尋次數
  5. 若找不到該字元,輸出:
    • Not found
    • 搜尋次數

Input / Output

Sample 1 Sample 2 Sample 3
Input p b a
Output Position: 4
Search count: 5
Not found
Search count: 13
Position: 0
Search count: 1

Comments

There are no comments at the moment.