-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
34 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
use std::io; | ||
|
||
fn linear_search(arr: &[i32], x: i32) -> i32 { | ||
for (index, &item) in arr.iter().enumerate() { | ||
if item == x { | ||
return index as i32; | ||
} | ||
} | ||
} | ||
|
||
pub fn run() { | ||
let arr = [2, 3, 4, 10, 40]; | ||
println!("Nhập giá trị x cần tìm kếm:"); | ||
|
||
let mut x = String::new(); | ||
io::stdin().read_line(&mut x) | ||
.expect("Lỗi khi đọc giá trị x"); | ||
|
||
let x: i32 = x.trim().parse() | ||
.expect("Lỗi khi chuyển đổi giá trị x thành số"); | ||
|
||
let result = linear_search(&arr, x); | ||
if result == -1 { | ||
println!("Phần tử không tồn tại trong mảng"); | ||
} else { | ||
println!("Phần tử được tìm thấy tại chỉ số: {}", result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod fibonacci; | ||
pub mod fibonacci; | ||
pub mod linear_search; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters