FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python-for-coding-test/7/3.java at master · ndb796/python-for-coding-test · GitHub
ndb796
/
python-for-coding-test
Public
Notifications
You must be signed in to change notification settings
Fork
831
Star
2.4k
Code
Issues
129
Pull requests
1
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
python-for-coding-test
/
7
/
3.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
42 lines (35 loc) · 1.32 KB
Breadcrumbs
python-for-coding-test
/
7
/
3.java
Copy path
File metadata and controls
42 lines (35 loc) · 1.32 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import
java
.
util
.*;
public
class
Main
{
// 이진 탐색 소스코드 구현(반복문)
public
static
int
binarySearch
(
int
[]
arr
,
int
target
,
int
start
,
int
end
) {
while
(
start
<=
end
) {
int
mid
= (
start
+
end
) /
2
;
// 찾은 경우 중간점 인덱스 반환
if
(
arr
[
mid
] ==
target
)
return
mid
;
// 중간점의 값보다 찾고자 하는 값이 작은 경우 왼쪽 확인
else
if
(
arr
[
mid
] >
target
)
end
=
mid
-
1
;
// 중간점의 값보다 찾고자 하는 값이 큰 경우 오른쪽 확인
else
start
=
mid
+
1
;
}
return
-
1
;
}
public
static
void
main
(
String
[]
args
) {
Scanner
sc
=
new
Scanner
(
System
.
in
);
// 원소의 개수(n)와 찾고자 하는 값(target)을 입력받기
int
n
=
sc
.
nextInt
();
int
target
=
sc
.
nextInt
();
// 전체 원소 입력받기
int
[]
arr
=
new
int
[
n
];
for
(
int
i
=
0
;
i
<
n
;
i
++) {
arr
[
i
] =
sc
.
nextInt
();
}
// 이진 탐색 수행 결과 출력
int
result
=
binarySearch
(
arr
,
target
,
0
,
n
-
1
);
if
(
result
== -
1
) {
System
.
out
.
println
(
"원소가 존재하지 않습니다."
);
}
else
{
System
.
out
.
println
(
result
+
1
);
}
}
}
Back
|
FazBrowse Home
|
New Git URL