[Algorithm] BOJ2562 - 최댓값
목차
1. 환경
Editor: Microsoft Visual Studio Code
Compiler: C++20
2. 문제
3. 접근법
알고리즘 분류
- 구현
최댓값과 최댓값을 가진 배열의 Index를 출력해준다.
시간을 줄이기 위해 입력을 받을 때마다 값을 갱신한다.
4. Library
bits/stdc++.h - Link2
(GNU C++ Standard Library Header - X)
(GCC Compiler Header - O)
5. Code
#include <bits/stdc++.h>
using namespace std;
int maxval, idx;
void input();
void func();
void output();
int main(void)
{
    input();
    func();
    output();
}
void input()
{
    int arr[9];
    for(int i = 0; i < 9; i++)
    {
        cin >> arr[i];
        if(maxval < arr[i])
        {
            maxval = arr[i];
            idx = i + 1;
        }
    }
}
void func()
{
    
}
void output()
{
    cout << maxval << '\n';
    cout << idx << '\n';
}
6. 정리
이번 문제의 경우 입력받은 후 검사하는 방법도 있다.
이는 시간복잡도가 N -> 2N으로 변한다.
N의 크기가 9밖에 되지 않아 영향은 없다봐도 무방하다.
시간복잡도에 있어 상수 배수는 무시하고 표현한다.
하지만 문제의 크기, 혹은 알고리즘의 시간복잡도가 큰 경우,
상수는 무시할 수 없는 부분이다.
이 점에 유의하며 코드를 짜는 습관을 들여야겠다.
7. Link
1. BOJ2562 - 최댓값
https://www.acmicpc.net/problem/2562
2. bits/stdc++.h
https://gcc.gnu.org/onlinedocs/gcc-4.8.0/libstdc++/api/a01541_source.html
3. Cplusplus.com
https://www.cplusplus.com/reference/
