File tree Expand file tree Collapse file tree 9 files changed +265
-0
lines changed Expand file tree Collapse file tree 9 files changed +265
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < bits/stdc++.h>
2
+
3
+ using namespace std ;
4
+
5
+ // 순차 탐색 소스코드 구현
6
+ int sequantialSearch (int n, string target, vector<string> arr) {
7
+ // 각 원소를 하나씩 확인하며
8
+ for (int i = 0 ; i < n; i++) {
9
+ // 현재의 원소가 찾고자 하는 원소와 동일한 경우
10
+ if (arr[i] == target) {
11
+ return i + 1 ; // 현재의 위치 반환 (인덱스는 0부터 시작하므로 1 더하기)
12
+ }
13
+ }
14
+ return -1 ; // 원소를 찾지 못한 경우 -1 반환
15
+ }
16
+
17
+ int n; // 원소의 개수
18
+ string target; // 찾고자 하는 문자열
19
+ vector<string> arr;
20
+
21
+ int main (void ) {
22
+ cout << " 생성할 원소 개수를 입력한 다음 한 칸 띄고 찾을 문자열을 입력하세요." << ' \n ' ;
23
+ cin >> n >> target;
24
+
25
+ cout << " 앞서 적은 원소 개수만큼 문자열을 입력하세요. 구분은 띄어쓰기 한 칸으로 합니다." << ' \n ' ;
26
+ for (int i = 0 ; i < n; i++) {
27
+ string x;
28
+ cin >> x;
29
+ arr.push_back (x);
30
+ }
31
+
32
+ // 순차 탐색 수행 결과 출력
33
+ cout << sequantialSearch (n, target, arr) << ' \n ' ;
34
+ }
Original file line number Diff line number Diff line change
1
+ #include < bits/stdc++.h>
2
+
3
+ using namespace std ;
4
+
5
+ // 이진 탐색 소스코드 구현(재귀 함수)
6
+ int binarySearch (vector<int > arr, int target, int start, int end) {
7
+ if (start > end) return -1 ;
8
+ int mid = (start + end) / 2 ;
9
+ // 찾은 경우 중간점 인덱스 반환
10
+ if (arr[mid] == target) return mid;
11
+ // 중간점의 값보다 찾고자 하는 값이 작은 경우 왼쪽 확인
12
+ else if (arr[mid] > target) return binarySearch (arr, target, start, mid - 1 );
13
+ // 중간점의 값보다 찾고자 하는 값이 큰 경우 오른쪽 확인
14
+ else return binarySearch (arr, target, mid + 1 , end);
15
+ }
16
+
17
+ int n, target;
18
+ vector<int > arr;
19
+
20
+ int main (void ) {
21
+ // n(원소의 개수)와 target(찾고자 하는 값)을 입력 받기
22
+ cin >> n >> target;
23
+ // 전체 원소 입력 받기
24
+ for (int i = 0 ; i < n; i++) {
25
+ int x;
26
+ cin >> x;
27
+ arr.push_back (x);
28
+ }
29
+ // 이진 탐색 수행 결과 출력
30
+ int result = binarySearch (arr, target, 0 , n - 1 );
31
+ if (result == -1 ) {
32
+ cout << " 원소가 존재하지 않습니다." << ' \n ' ;
33
+ }
34
+ else {
35
+ cout << result + 1 << ' \n ' ;
36
+ }
37
+ }
Original file line number Diff line number Diff line change
1
+ #include < bits/stdc++.h>
2
+
3
+ using namespace std ;
4
+
5
+ // 이진 탐색 소스코드 구현(반복문)
6
+ int binarySearch (vector<int > arr, int target, int start, int end) {
7
+ while (start <= end) {
8
+ int mid = (start + end) / 2 ;
9
+ // 찾은 경우 중간점 인덱스 반환
10
+ if (arr[mid] == target) return mid;
11
+ // 중간점의 값보다 찾고자 하는 값이 작은 경우 왼쪽 확인
12
+ else if (arr[mid] > target) end = mid - 1 ;
13
+ // 중간점의 값보다 찾고자 하는 값이 큰 경우 오른쪽 확인
14
+ else start = mid + 1 ;
15
+ }
16
+ return -1 ;
17
+ }
18
+
19
+ int n, target;
20
+ vector<int > arr;
21
+
22
+ int main (void ) {
23
+ // n(원소의 개수)와 target(찾고자 하는 값)을 입력 받기
24
+ cin >> n >> target;
25
+ // 전체 원소 입력 받기
26
+ for (int i = 0 ; i < n; i++) {
27
+ int x;
28
+ cin >> x;
29
+ arr.push_back (x);
30
+ }
31
+ // 이진 탐색 수행 결과 출력
32
+ int result = binarySearch (arr, target, 0 , n - 1 );
33
+ if (result == -1 ) {
34
+ cout << " 원소가 존재하지 않습니다." << ' \n ' ;
35
+ }
36
+ else {
37
+ cout << result + 1 << ' \n ' ;
38
+ }
39
+ }
Original file line number Diff line number Diff line change
1
+ #include < bits/stdc++.h>
2
+
3
+ using namespace std ;
4
+
5
+ // 이진 탐색 소스코드 구현(반복문)
6
+ int binarySearch (vector<int > arr, int target, int start, int end) {
7
+ while (start <= end) {
8
+ int mid = (start + end) / 2 ;
9
+ // 찾은 경우 중간점 인덱스 반환
10
+ if (arr[mid] == target) return mid;
11
+ // 중간점의 값보다 찾고자 하는 값이 작은 경우 왼쪽 확인
12
+ else if (arr[mid] > target) end = mid - 1 ;
13
+ // 중간점의 값보다 찾고자 하는 값이 큰 경우 오른쪽 확인
14
+ else start = mid + 1 ;
15
+ }
16
+ return -1 ;
17
+ }
18
+
19
+ // N(가게의 부품 개수)와 M(손님이 확인 요청한 부품 개수)
20
+ int n, m;
21
+ // 가게에 있는 전체 부품 번호들
22
+ vector<int > arr;
23
+ // 손님이 확인 요청한 부품 번호들
24
+ vector<int > targets;
25
+
26
+ int main (void ) {
27
+ cin >> n;
28
+ for (int i = 0 ; i < n; i++) {
29
+ int x;
30
+ cin >> x;
31
+ arr.push_back (x);
32
+ }
33
+ cin >> m;
34
+ for (int i = 0 ; i < m; i++) {
35
+ int target;
36
+ cin >> target;
37
+ targets.push_back (target);
38
+ }
39
+ // 손님이 확인 요청한 부품 번호를 하나씩 확인
40
+ for (int i = 0 ; i < m; i++) {
41
+ // 해당 부품이 존재하는지 확인
42
+ int result = binarySearch (arr, targets[i], 0 , n - 1 );
43
+ if (result != -1 ) {
44
+ cout << " yes" << ' ' ;
45
+ }
46
+ else {
47
+ cout << " no" << ' ' ;
48
+ }
49
+ }
50
+ }
Original file line number Diff line number Diff line change
1
+ #include < bits/stdc++.h>
2
+
3
+ using namespace std ;
4
+
5
+ // N(가게의 부품 개수)와 M(손님이 확인 요청한 부품 개수)
6
+ int n, m;
7
+ int arr[1000001 ];
8
+ vector<int > targets;
9
+
10
+ int main (void ) {
11
+ cin >> n;
12
+ for (int i = 0 ; i < n; i++) {
13
+ int x;
14
+ cin >> x;
15
+ arr[x] = 1 ;
16
+ }
17
+ cin >> m;
18
+ for (int i = 0 ; i < m; i++) {
19
+ int target;
20
+ cin >> target;
21
+ targets.push_back (target);
22
+ }
23
+ // 손님이 확인 요청한 부품 번호를 하나씩 확인
24
+ for (int i = 0 ; i < m; i++) {
25
+ // 해당 부품이 존재하는지 확인
26
+ if (arr[targets[i]] == 1 ) {
27
+ cout << " yes" << ' ' ;
28
+ }
29
+ else {
30
+ cout << " no" << ' ' ;
31
+ }
32
+ }
33
+ }
Original file line number Diff line number Diff line change
1
+ #include < bits/stdc++.h>
2
+
3
+ using namespace std ;
4
+
5
+ // N(가게의 부품 개수)와 M(손님이 확인 요청한 부품 개수)
6
+ int n, m;
7
+ // 가게게 있는 전체 부품 번호를 담을 집합(set) 컨테이너
8
+ set<int > s;
9
+ vector<int > targets;
10
+
11
+ int main (void ) {
12
+ cin >> n;
13
+ for (int i = 0 ; i < n; i++) {
14
+ int x;
15
+ cin >> x;
16
+ s.insert (x);
17
+ }
18
+ cin >> m;
19
+ for (int i = 0 ; i < m; i++) {
20
+ int target;
21
+ cin >> target;
22
+ targets.push_back (target);
23
+ }
24
+ // 손님이 확인 요청한 부품 번호를 하나씩 확인
25
+ for (int i = 0 ; i < m; i++) {
26
+ // 해당 부품이 존재하는지 확인
27
+ if (s.find (targets[i]) != s.end ()) {
28
+ cout << " yes" << ' ' ;
29
+ }
30
+ else {
31
+ cout << " no" << ' ' ;
32
+ }
33
+ }
34
+ }
Original file line number Diff line number Diff line change
1
+ #include < bits/stdc++.h>
2
+
3
+ using namespace std ;
4
+
5
+ // 떡의 개수(N)와 요청한 떡의 길이(M)
6
+ int n, m;
7
+ // 각 떡의 개별 높이 정보
8
+ vector<int > arr;
9
+
10
+ int main (void ) {
11
+ cin >> n >> m;
12
+ for (int i = 0 ; i < n; i++) {
13
+ int x;
14
+ cin >> x;
15
+ arr.push_back (x);
16
+ }
17
+ // 이진 탐색을 위한 시작점과 끝점 설정
18
+ int start = 0 ;
19
+ int end = 1e9 ;
20
+ // 이진 탐색 수행 (반복적)
21
+ int result = 0 ;
22
+ while (start <= end) {
23
+ long long int total = 0 ;
24
+ int mid = (start + end) / 2 ;
25
+ for (int i = 0 ; i < n; i++) {
26
+ // 잘랐을 때의 떡의 양 계산
27
+ if (arr[i] > mid) total += arr[i] - mid;
28
+ }
29
+ if (total < m) { // 떡의 양이 부족한 경우 더 많이 자르기(왼쪽 부분 탐색)
30
+ end = mid - 1 ;
31
+ }
32
+ else { // 떡의 양이 충분한 경우 덜 자르기(오른쪽 부분 탐색)
33
+ result = mid; // 최대한 덜 잘랐을 때가 정답이므로, 여기에서 result에 기록
34
+ start = mid + 1 ;
35
+ }
36
+ }
37
+ cout << result << ' \n ' ;
38
+ }
You can’t perform that action at this time.
0 commit comments