본문 바로가기
  • Let's study

C++54

[알고리즘] 이분 탐색 이분 탐색 알고리즘이란? 정렬되어 있는 리스트에서 탐색 범위를 절반씩 줄여가며 원하는 데이터를 탐색하는 알고리즘이다. 정렬된 리스트에서 중간 값을 선택하고 선택한 값과 찾고자 하는 값의 크고 작음을 비교한다. 선택한 중간값이 만약 찾는 값보다 크면 그 값은 새로운 최댓값이 되며, 작으면 그 값은 새로운 최솟값이 된다. 꼭 정렬해야함을 잊지 말자! 이분 탐색 알고리즘 구현 - C++ 이분 탐색은 O(log N) 의 시간복잡도를 가진다. #include using namespace std; int main(){ int arr[10]={1,2,3,4,5,6,7,8,9}; int target=6; int low=0, high=9;//시작 인덱스, 끝 인덱스 int res=0; //이분탐색 부분 while(low.. 2023. 10. 1.
[C++] 백준 25828: Corona Virus Testing 문제 https://www.acmicpc.net/problem/25828 25828번: Corona Virus Testing Testing for Corona can be done individually, e.g., 100 people require 100 test kits. Alternatively, the test can be done in groups (pools), e.g., 100 people can be divided into five group of 20 people each and then using only one test kid per group. If one www.acmicpc.net 해결방법 그룹별 검사하는 데 사용되는 키트 수는 그룹 수 + 그룹별 인원수 * 양성인 그룹 수이고 .. 2023. 5. 22.
[C++] 백준 25881: Electric Bill 문제 https://www.acmicpc.net/problem/25881 25881번: Electric Bill The first input line contains two integers (each between 2 and 20, inclusive), indicating the rate/KWH for the first 1000 KWH and the rate/KWH for the additional usage, respectively. The next input line contains a positive integer, n, indicating the number www.acmicpc.net 해결방법 입력받은 고객의 에너지 소비량에서 1000은 기본 요율, 1000을 뺀 나머지는 추가 요율을 적용하여 .. 2023. 5. 21.
[C++] 백준 9772: Quadrants 문제 https://www.acmicpc.net/problem/9772 9772번: Quadrants Given the coordinates (x,y) of some points in 2-dimensional plane, find out which quadrant(Q1-Q4) the points are located. If a point is located on X-axis or Y-axis, print out AXIS instead. www.acmicpc.net 해결방법 x와 y가 양수인지 음수인지로 어느 사분면인지 출력하고 축이면 AXIS를 출력한다. 마지막 0 0일 때도 AXIS를 출력해야 한다. 코드 #include using namespace std; int main() { ios_base::s.. 2023. 5. 20.
[C++] 백준 25893: Majestic 10 문제 https://www.acmicpc.net/problem/25893 25893번: Majestic 10 The movie “Magnificent 7” has become a western classic. Well, this year we have 10 coaches training the UCF programming teams and once you meet them, you’ll realize why they are called the “Majestic 10”! The number 10 is actually special in many www.acmicpc.net 해결방법 1. 모두 10 미만이면 'zilch' 출력 2. 모두 10 이상이면 'triple-double' 출력 3. 두 개만 10 이.. 2023. 5. 19.
[C++] 백준 27880: Gahui and Soongsil University statio 문제 https://www.acmicpc.net/problem/27880 27880번: Gahui and Soongsil University station Soongsil University Station is famous as the deepest station on Seoul Subway Line 7. This station is so deep that the platform is on the B6. Gahui was surprised that she did not reach the platform after more than five minutes from the exit. Gahui wants to www.acmicpc.net 해결방법 에스컬레이터에서 1 step의 높이는 21cm, 계단에서 1 .. 2023. 5. 18.