Programming 기초
-
[c++ 백준 2606 ] 인접리스트, 인접행렬 풀이Programming 기초/Coding Test 2024. 9. 29. 19:47
인접행렬 풀이#include #include using namespace std;void dfs(vector> &v, const int N, const int node, vector &visited){ if (!visited[node]) visited[node] = 1; else return; for (int i = 1; i > N >> M; vector visited(N+1); vector> v(N+1, vector(N+1,0)); // 인접행렬 for (int i = 0 ; i > s >> e; v[s][e] = 1; v[e][s] = 1; } dfs(v, N, 1, visited); int cnt = 0;..
-
c++ 백준 1676번 팩토리얼의 0의 개수Programming 기초/C++ 2024. 8. 20. 11:16
팩토리얼은 오버플로우가 생기기 쉽다. n이 500 까지이므로 팩토리얼을 계산한 후에 10으로 나누어 0의 개수를 세는 방법은 적절치 않다.대체 방법으로 모든 수를 소인수 분해해서 5의 거듭제곱이 각각 몇 개 있는지 파악하는 방식이 있다.#include using namespace std;int countZeros(int n){ int count = 0; for (int i = 5; n / i > 0; i *= 5) { count += n / i; } return count;}int main(int argc, char **argv){ int N; cin >> N; int cnt = countZeros(N); cout 10!을 생각해보자.10*..
-
STL 컨테이너Programming 기초/C++ 2024. 8. 11. 15:41
* 컨테이너컨테이너는 같은 타입을 저장, 관리할 목적으로 만들어진 클래스(총 7가지 컨테이너 제공) - 표준 시퀀스 컨테이너(standard sequence container): 컨테이너 원소가 자신만의 삽입 위치(순서)를 가지는 컨테이너vector, deque, list: 선형적 - 표준 연관 컨테이너(standard associative container): 저장 원소가 삽입 순서와 다르게 특정 정렬 기준에 의해 자동 정렬되는 컨테이너set, multiset, map, multimap: 비선형적 * 데이터를 하나의 연속한 메모리 단위로 저장하느냐에 따라 컨테이너를 분류 - 배열 기반 컨테이너(arrray-based container): 데이터 여러 개가 하나의 메모리 단위에 저장vector, deq..
-
[bj 8958] c++, cin 다음에 cin.getline 사용할 시 cin.ignore() 해주기Programming 기초/Coding Test 2024. 7. 19. 16:29
https://www.acmicpc.net/problem/8958 동적할당을 이용한 문자열을 연습하기 위해 아래와 같이 코드를 짜보았다.#include using namespace std;int main(int argc, char **argv){ int n; cin >> n; cin.ignore(); for (int i = 0; i 주의해야 할 점cin은 버퍼에 '\n'이 남아있어서 cin.getline() 하기 전에 cin.ignore()로 버퍼를 비워주지 않으면 cin 직후의 cin.getline에 '\n'이 입력되어버린다.처음에는 아무생각없이 cin.getline 직전에 cin.ignore()를 적용했으나, cin.ignore()는 디폴트값이 버퍼의 1개를 지우는 것이므로 c..
-
[c++] vscode에서 c++ 설치 the prelaunchtask c/c++: gcc build active file terminated with exit code -1 에러 해결Programming 기초/C++ 2024. 7. 19. 15:50
https://www.youtube.com/watch?v=UqCZda8DLGc&t=9792s 위 영상을 참고했다. https://code.visualstudio.com/docs/cpp/config-mingw먼저 g++을 설치해야하는데, window에 경우 MinGW를 통해 설치한다. https://www.msys2.org/사이트에서 아래 설치파일을 다운받아서 실행하고 기본 설치위치 그대로 쭉 설치해준다. 설치가 완료되면 MSYS2 터미널 창이 뜬다. pacman -S --needed base-devel mingw-w64-ucrt-x86_64-toolchain 명령어 입력 후 default all 머시기 뜨면 enter 누르고y/n 뜨면 y 눌러서 쭉 진행해준다. 마지막에 g++ 제대로 설치되었는지, 버전..
-
[c++] 참조자형식 "&"Programming 기초/C++ 2024. 7. 19. 10:47
* "&"는 참조자 형식(reference )이다. 형식 &이름 = 원본;의 형태로 쓴다.- 선언과 동시에 반드시 초기화해줘야 한다.#includeusing namespace std;void TestFunc(int &rParam){ rParam=100;}int main(int argc, char **argv){ int nData = 10; int &ref = nData; ref = 20; cout 주소를 알려주는 역할이라고 보면 될 듯 하다. 함수에서 주소로 받고자 할 때 많이 사용되고, 아래와 같이 for 문에서도 활용된다. #includeusing namespace std;int main(int argc, char **argv){ int aList[5] = {..
-
[bj 3052] c++ 풀이 및 bits/stdc++.hProgramming 기초/Coding Test 2024. 7. 19. 10:08
https://www.acmicpc.net/problem/3052 내 풀이#includeusing namespace std;int main(int argc, char** argv){ ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int array[43]={0}; int tmp; int cnt=0; while (cin >>tmp) { int remain = tmp%42; if (!array[remain]){ array[remain]=1; ++cnt; } } cout 찾아본 풀이#includeusing nam..
-
[bj 10809] c++ 풀이 -> 문자열 동적할당 직접 구현하기Programming 기초/Coding Test 2024. 7. 12. 19:27
https://www.acmicpc.net/problem/10809#include#includeusing namespace std;int main(int argc, char** argv){ int cnt[26] = { 0 , }; string s; cin >> s; for (char i = 'a'; i 코드 출처 : https://cryptosalamander.tistory.com/11std::string과 std::find를 사용하면 쉽게 구현할 수 있는데, string을 문자열 동적할당을 이용해서 직접 구현해보았다. #includeusing namespace std;// 문자열의 길이를 반환하는 함수int slen(const char* str) { int index = 0; w..