삼성 SW 역량테스트 B형 대비(~3/14) #16
Replies: 4 comments
-
|
Beta Was this translation helpful? Give feedback.
0 replies
-
이분탐색// target 값이 몇 개 있는지
cout << upper_bound(A, A+N, target)-lower_bound(A, A + N, target);// target보다 작은값 중 가장 오른쪽에 있는 인덱스 찾기
const int len = 10;
//int a[len] = { 2, 4, 6, 10, 10, 16, 16, 16, 30, 32 };
int a[len] = { 2, 7, 11, 11, 16, 19, 22, 22, 22, 32 };
int func(int target) {
int st, en, mid;
st = -1; // target이 a[0] 이하면 -1을 반환해야 해서
en = len - 1;
while (st < en) {
mid = (st + en + 1) / 2; // 무한루푸에 빠지지 않게
if (a[mid] >= target) en = mid - 1;
else st = mid;
}
return st;
}
int main() {
cout << func(12); // 추가할 숫자가 12
return 0;
} |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
|
Beta Was this translation helpful? Give feedback.
0 replies
-
|
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment

Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Beta Was this translation helpful? Give feedback.
All reactions