z034: ch3-3-循序搜尋實作
標籤 : 循序搜尋
通過比率 : 20人/21人 ( 95% ) [非即時]
評分方式:
Tolerant

最近更新 : 2022-07-15 16:57

內容

使用循序搜尋法,從數列 3,6,1,8,4,5,7,2 中進行關鍵值搜尋。找到時,輸出它的位置(即在陣列中的索引值),否則輸出「not found」。 

 

輸入說明

以陣列存放數列 3,6,1,8,4,5,7,2,並輸入一個正整數 n

輸出說明

找到時,輸出它的位置(即在陣列中的索引值),否則輸出「not found」

範例輸入 #1
2
範例輸出 #1
7
範例輸入 #2
9
範例輸出 #2
not found
測資資訊:
記憶體限制: 64 MB
公開 測資點#0 (50%): 1.0s , <1K
公開 測資點#1 (50%): 1.0s , <1K
提示 :

#include <iostream>
using namespace std;

int main() {
int a[8] = {3,6,1,8,4,5,7,2};
int i,k,found=0;

cin >>k;
for (i=0; i<8; i++){
if (a[i]==k){
cout <<i;
found = 1;
break;
}
}
if (found ==0)
cout <<"not found";

return 0;
}

 

 

a = [3,6,1,8,4,5,7,2]
found = 0
w = input()
k = int(w)
for i in range(len(a)):
if a[i] == k:
print(i)
found = 1;
break;

if found == 0:
print('not found')

標籤:
循序搜尋
出處:
全華高中資訊科技第三章 [管理者:
alice2100che... (alice2100chen)
]


編號 身分 題目 主題 人氣 發表日期
沒有發現任何「解題報告」