코딩테스트
[골5] 14503 - 로봇 청소기
수정된 코드 #include #define MAX 51 #define y first #define x second using namespace std; struct Robot { int y, x, d; Robot() = default; Robot(int y, int x, int d = 0) : y(y), x(x), d(d) { } } robot; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL), cout.tie(NULL); int h, w; cin >> h >> w; cin >> robot.y >> robot.x >> robot.d; auto& y = robot.y, & x = robot.x, & d = robot.d; if (d == 1) d..
[실3] 1966 - 프린터 큐
#include #include #include #include using namespace std; #define y front().first #define x front().second int main() { ios_base::sync_with_stdio(0); cin.tie(0), cout.tie(0); int tc; cin >> tc; for (int i = 0; i > t >> f; queue q; priority_queue pq; for (int j = 0; j > val; q.push({ j,val }); pq.push(val); } ..
[실3] 1021 - 회전하는 큐
#include #include #include #include using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0), cout.tie(0); int s, n, cnt = 0; cin >> s >> n; deque dq; for (int i = 1; i > num; for (int i = 0; i < s; i++) { if (dq[i] == num) { idx = i; break; } } for (int i = 0; i < dq.size(); i++) { if (dq.front() == num) { dq.pop_front(); break; } if (idx < dq.size() / 2 + 1) { dq.push_back(dq..
[골5] 2023 - 신기한 소수
#include #include #include #include using namespace std; int n = 0; bool Prime(int num) { for (int i = 2; i * i
[골2] 10800 - 컬러볼
#include #include #include #include using namespace std; struct sColorBall { int n, color, size; sColorBall() = default; sColorBall(int n, int color, int size):n(n),color(color),size(size) { } }; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; vector vecBall(n); for (int i = 0; i > vecBall[i].color >> vecBall[i].size; } sort(vecB..
구조체 또는 클래스 관련 정렬 (sort 함수)
구조체 또는 클래스는 항상 연산자 오버로딩을 해줘야하고 클래스명으로 가야한다. struct Student { int id, pow; char team; Student(int a, char b, int c) : id(a),team(b),pow(c) { } bool operator(const Student& b) const { return pow > b.pow; } }; 여기서 pow 기준으로 정렬할텐데 이때 algorithm 헤더파일 내에 sort 함수를 사용하고 pred 인자로 해당 연산자 오버로딩 함수를 보내주면 된다. 디폴트 값 : less(); vector st; // pred 템플릿 인자로 무조건 클래스명으로 줘야한다. sort(st.begin(), st.end(), greater()); less..
문자열 (소문자 대문자 변환) transform 함수
algorithm 헤더파일 내에 있다 toupper (대문자), tolower(소문자) 그리고 :: 익명 네임스페이스 무조건 붙여야한다. #include #include #include using namespace std; int main() { string str1, str2; cin >> str1 >> str2; transform(str1.begin(), str1.end(), str1.begin(), ::toupper); transform(str2.begin(), str2.end(), str2.begin(), ::tolower); cout
최솟값과 최댓값 표현하기
#include #include // 자료형의 최댓값과 최솟값이 정의된 헤더 파일 int main() { char num1 = CHAR_MIN; // char의 최솟값 short num2 = SHRT_MIN; // short의 최솟값 int num3 = INT_MIN; // int의 최솟값 long num4 = LONG_MIN; // long의 최솟값 long long num5 = LLONG_MIN; // long long의 최솟값 // char, short, int는 %d로 출력하고 long은 %ld로 출력, long long은 %lld로 출력 printf("%d %d %d %ld %lld\n", num1, num2, num3, num4, num5); // -128 -32768 -2147483648 -..
[1] - 2016년
2016년 1월 1일은 금요일이므로 배열은 금요일부터 시작day는 2016년 1~12월까지에 대한 일수다.공식으로 간단하게 풀 수 있는 문제다.#include #include using namespace std;string solution(int a, int b) { string week[7]{"FRI","SAT","SUN","MON","TUE","WED","THU"}; int day[]{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int val=0; for(int i=0;i week[4] = "TUE"}
[3] 네트워크
#include #include using namespace std; bool vis[1000]{false}; void dfs(vector computers, int start) { vis[start] = true; for (int i = 0; i < computers[start].size(); i++) { if (!vis[i] && computers[start][i]) dfs(computers, i); } } int solution(int n, vector computers) { int answer = 0; for (int i = 0; i < n; i++) { if(!vis[i]) { dfs(computers, i); answer++; } } return answer; }