본문 바로가기

Algorithm

(2139)
C++(씨쁠쁠)(cplusplus)-백준(baekjoon)(BaekJoon)코딩 10819번:차이를 최대로 답 next_permutation()함수를 사용하여 풀었습니다.1.먼저 sort()함수를 이용해 오름차순으로 정렬해줍니다.2.다음 순열이 있다면 그 배열을 다음 순열(집합이 작은순)로 바꿔주고 true를 반환합니다.3.반대의 경우는 false를 반환합니다. 123456789101112131415161718#include#includeusing namespace std;int n, a[8], big, sum;int main() { scanf("%d", &n); for (int i = 0; i
C++(씨쁠쁠)(cplusplus)-백준(baekjoon)(BaekJoon)코딩 10999번:구간 합 구하기 2 답 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788#include #include #include using namespace std;//ary :입력받는 배열//tree : 새그먼트 트리//node : 트리의 노드 번호//start~end까지의 구간의 합 구하기.long long init(vector &ary, vector &tree,int node,int start,int end){ if (start == end) return tree[node] = ary..
(C++) - 백준(BOJ) 2669 : 직사각형 네개의 합집합의 면적 구하기 답 #include using namespace std; int a[101][101]; int lx, ly, rx, ry, ans; int main() { for (int k = 1; k > lx >> ly >> rx >> ry; for (int i = 100-ry; i < 100 - ly; i++) { for (int j = lx; j < rx; j++) { a[i][j] = 1; } } } for(int i = 0; i
C++(씨쁠쁠)(cplusplus)-백준(baekjoon)(BaekJoon)코딩 2812번:크게 만들기 답 12345678910111213141516171819202122232425262728#include #include #include #include using namespace std;char str[500001];char result[500001];int main() { ios::sync_with_stdio(false); int N, K; cin >> N >> K >> str; int L = N - K; int len = 0; for (int i = 0; i
C++(씨쁠쁠)(cplusplus)-백준(baekjoon)(BaekJoon)코딩 2812번:크게 만들기(스택) 답 123456789101112131415161718192021222324252627282930313233343536#include #include #include using namespace std;char s[500001];stack t, tmp;int main() { ios_base::sync_with_stdio(false); int n, k, i; cin >> n >> k >> s; int slen = strlen(s); for (i = 0; k;) { if (!t.empty() && (t.top()
C++(씨쁠쁠)(cplusplus)-백준(baekjoon)(BaekJoon)코딩 2331번:반복수열 답 123456789101112131415161718192021222324252627282930313233#include using namespace std;int c[1000000];int A, P;int pow(int x, int P){ int ans = 1; for (int i = 0; i 0) { ans += pow(A % 10, P); A /= 10; } return ans;}int sequence(int A, int P, int ans){ if (c[A] != 0)//수열이 반복된다면 그 전 숫자까지가 수열의 길이 return c[A]-1; c[A] = ans; int next_sequence = next(A, P); return sequence(next_sequence, P, ans + 1);}..
C++(씨쁠쁠)(cplusplus)-백준(baekjoon)(BaekJoon)코딩 1707번:이분 그래프(DFS) 답 1.이분 그래프는 한 정점으로 이동할 때 다른 집합에 속한 정점으로 이동하게 된다.2.따라서 한번 이동할 때마다 다른 집합으로 체크해준다.3.체크해준 숫자가 다르다면 이분그래프이다.1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253#include #include using namespace std;vector a[20001];int ck[20001];int K, V, E, u, v; void DFS(int x, int c){ ck[x] = c; for (int i = 0; i 다른 집합의 정점으로 이동 } }}int main() { cin >> K; while (K--) { cin ..
C++(씨쁠쁠)(cplusplus)-백준(baekjoon)(BaekJoon)코딩 10867번:중복 빼고 정렬하기 답 123456789101112131415161718#include #include using namespace std;int n, a[100000],c;int main() { cin >> n; for (int i = 0; i > a[i]; sort(a, a + n); c = a[0]; cout