#include using namespace std;typedef long long ll;int n;vectorv;void dfs(ll num){ if(num > 9876543210) return; v.push_back(num); for(int i=0; i i) { dfs(num*10+i); } }}int main(void){ ios_base::sync_with_stdio(false); cin.tie(0); cin >> n; for(int i=0; i 재귀를 통해 풀 수 있는 문제이다.처음에 기본 시작인 0~9까지를 dfs로 돌려준다.여기에 이어서 숫자를 하나씩 붙여주는데,num%10>i 조건문을 통해 숫자의 마지막 ..
#include #include #include #include using namespace std;long long cal(long long a, long long b, char op){ if(op == '+' ) return a+b; else if(op == '-') return a-b; else return a*b;}long long solution(string expression) { long long answer = 0; vectorv = {'*', '+', '-'}; vectornum; vectorop; string tmp = ""; for(int i=0; i= '0' && expression[i] num2 = num; ve..
문제링크 : https://www.acmicpc.net/problem/1063#include using namespace std;//R, L, B, T, RT, LT, RB, LBmap m = { {"R", 0}, {"L", 1}, {"B", 2}, {"T", 3}, {"RT", 4}, {"LT", 5}, {"RB", 6}, {"LB", 7}};int dy[] = {0, 0, -1, 1, 1, 1, -1, -1};int dx[] = {1, -1, 0, 0, 1, -1, 1, -1};int main(void){ ios_base::sync_with_stdio(false); cin.tie(0); string king, stone; int n; cin>> king >> st..
#include #include #include #include using namespace std;bool check(string tmp){ stackst; for(auto i : tmp) { if(i == '(') st.push(i); else { if(st.empty()) return false; //올바르지 않은 문자열 st.pop(); } } return st.empty();}string solution(string p) { string answer = ""; string u = "", v = ""; if(p.empty()) return p; // 조건 1 ..
문제링크 : https://www.acmicpc.net/problem/1057#include using namespace std;int main(void){ ios_base::sync_with_stdio(false); cin.tie(0); int n, a, b, cnt = 0; cin >> n >> a >> b; while(a!=b) { a = (a+1)/2; b = (b+1)/2; cnt++; } cout 각 값들을 2로 나누면서 같아지는 순간을 체크한다.다만 단순히 2를 나누는 것이 아니라 1을 더해주어야 한다.7, 8을 예시로 들면 7/2 = 3, 8/2 = 4 이기에 둘 다 같은 4를 반환하려면 1을 더해줄 필요가 ..
#include #include using namespace std;vector solution(int n, long long k) { vector answer, v; vector fac(n, 1); //팩토리얼 계산 for (int i = 1; i = 0; i--) { int idx = k / fac[i]; //위치 answer.push_back(v[idx]); v.erase(v.begin() + idx); //찾은 값 삭제 k %= fac[i]; //k값 조정 } return answer;} 먼저 가능한 케이스를 통해 규칙을 찾아보자.1 : 12 : 1 2 / 2 13 : 1 2 3 / 1 3 2 / 2 1 3 /..
문제링크 : https://www.acmicpc.net/problem/1058#include using namespace std;int dist[51][51];int main(void){ ios_base::sync_with_stdio(false); cin.tie(0); int n, result = 0; cin >> n; for(int i=0; i> s; for(int j=0; j 플로이드 - 워셜 알고리즘을 통해 풀 수 있는 문제이다.먼저 배열을 최대값으로 지정하고, 이후에 'Y' 값을 기준으로 해당 거리 ij에 대해서 초기 거리 1을 설정해준다. 이제 플로이드 워셜 알고리즘을 통해서 가능한 케이스에 대해 거리를 갱신해준다.거리를 갱신했으면 이제 가장 2-친구의 수..
#include #include #include #include using namespace std;int dy[] = {0, 0, 1, -1};int dx[] = {1, -1, 0, 0};int visited[101][101];vector solution(vector maps) { vector answer; for(int i=0; i>q; q.push({i, j}); visited[i][j] = 1; int sum = maps[i][j]-'0'; //누적합용 변수, 시작 값 저장 while(!q.empty()) { auto [y, x] = q.front(); ..
#include #include #include #include #include using namespace std;string change(string in) { string out = ""; map m = { {"A#", 'H'}, {"B#", 'I'}, {"C#", 'J'}, {"D#", 'K'}, {"E#", 'L'}, {"F#", 'M'} }; //#을 대응되는 문자로 변환 for (int i = 0; i musicinfos) { string answer = "(None)"; int time = 0; //변환 m = change(m); for (int i = 0; i parts; string part..
#include #include #include using namespace std;int solution(vector players, int m, int k) { int answer = 0; queue q; for (int i = 0; i queue를 활용해 풀 수 있는 문제이다.우선 현재 플레이어 수와 m 값 기준으로 필요한 서버 수를 계산해준다.그리고 해당 문제에서 queue에 서버 수를 담아줄 것이므로 queue의 사이즈는 작동 중인 서버 수가 될 것이다. 이제 운영중인 서버수와 필요한 서버 수의 차이를 구해 추가해야할 서버 수가 몇개인지 구해주고, 이를 answer에 누적하여 더해준다.이제 추가된 서버 수 만큼 해당 서버의 종료시간을 queue에 넣어주자.매 반복문마다 처음에 ..
#include #include #include using namespace std;vector>v[51];vectordist;void Dijkstra(){ priority_queue, vector>, greater>>pq; pq.push({0, 1}); dist[1] = 0; while(!pq.empty()) { auto [cost, cur] = pq.top(); pq.pop(); for(int i=0; i cost + ncost) { dist[next] = cost + ncost; pq.push({dist[next], next}); ..
#include #include #include #include using namespace std;map m;void cal(string order, string tmp, int idx, int target) { if (tmp.size() == target) { m[tmp]++; return; } for (int i = idx; i solution(vector orders, vector course) { vector answer; for (auto& order : orders) sort(order.begin(), order.end()); for (auto i : course) { m.clear(); ..
#include #include #include #include using namespace std;int dy[] = {0, 0, 1, -1};int dx[] = {1, -1, 0, 0};int solution(vector maps) { int answer = -1; bool flag = false; int y = 0, x = 0; queue>q; vector> v(maps.size(), vector(maps[0].size(), 0)); for(int i = 0; i= maps[0].size() || ny = maps.size()) continue; if(maps[ny][nx] != 'X' && v[ny][nx] == 0) //X 여부 ..
#include #include #include using namespace std;int solution(vector arrayA, vector arrayB) { int answer = 0; int Anum = accumulate(arrayA.begin(), arrayA.end(), 0, gcd); int Bnum = accumulate(arrayB.begin(), arrayB.end(), 0, gcd); answer = max(Anum, Bnum); if(answer == Anum) { for(auto i : arrayB) { if(i % answer == 0) return 0; } } else ..
#include #include using namespace std;string solution(int n) { string answer = ""; char c[3] = {'4', '1', '2'}; while(n > 0) { int r = n % 3; answer = c[r] + answer; n /= 3; if(r == 0) n--; } return answer;} 3진법 계산을 응용한 문제이다. 이번 문제는 기존의 0, 1, 2가 아닌 아래와 같이 4, 1, 2를 나머지로 갖는다.n%3==0 : 4 / n%3==1 : 1 / n%3==2 : 2 따라서 이를 토대로 ans..