728x90
// 1. WeakSet
let messages = [
{text: "Hello", from: "John"},
{text: "How goes?", from: "John"},
{text: "See you soon", from: "Alice"}
];
const readMessages = new WeakSet();
// two messages have been read
readMessages.add(messages[0]);
readMessages.add(messages[1]);
// readMessages has 2 elements
// ...let's read the first message again!
readMessages.add(messages[0]);
// readMessages still has 2 unique elements
// answer: was the message[0] read?
console.log("Read message 0: " + readMessages.has(messages[0])); // true
messages.shift();
// now readMessages has 1 element (technically memory may be cleaned later)
// 2. Symbol
const isRead = Symbol("isRead");
messages[0][isRead] = true;
728x90

https://www.acmicpc.net/problem/2309

#include <bits/stdc++.h>
using namespace std;
int num[9], result[7];
int main(void) {
ios::sync_with_stdio(false), cin.tie(nullptr);
for (int i = 0; i < 9; i++) cin >> num[i];
for (int a = 0; a < 8; a++) { // all combination
int total = 0;
for (int b = a + 1; b < 9; b++) {
total = 0;
for (int c = 0, i = 0; c < 9; c++) {
if (c != a && c != b) result[i++] = num[c]; // key point
}
for (int i = 0; i < 7; i++) total += result[i];
if (total == 100) break;
}
if (total == 100) break;
}
sort(result, result + 7);
for (int i = 0; i < 7; i++) cout << result[i] << '\n';
}
// #include <bits/stdc++.h>
// #define N 9
// using namespace std;
// int main() {
// ios::sync_with_stdio(0), cin.tie(0);
// int a[N] = {}, sum = 0;
// for (int i = 0; i < N; i++) cin >> a[i], sum += a[i];
// sort(a, a + N);
// for (int i = 0; i < N; i++) {
// for (int j = i + 1; j < N; j++) {
// if (sum - a[i] - a[j] == 100) {
// for (int k = 0; k < 9; k++)
// if (k != i && k != j) cout << a[k] << '\n';
// return 0;
// }
// }
// }
// }
/*
일곱 난쟁이(2309)
O(N^3)
값을 입력 받은 후 바로 정렬하고 시작하는게 좋다 sort()
9명 전체 합을 구한 후 나머지 2명을 빼, 그 값이 100인지 확인하면 된다
모든 경우의 수를 고려하려 반복하면 된다(브루트 포스)
단, 해당 결과를 찾았다면 return 해주도록 하자
*/
view raw 2309.cpp hosted with ❤ by GitHub
728x90

https://www.acmicpc.net/problem/2587

#include <bits/stdc++.h>
#define N 5
using namespace std;
int main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
int a[N] = {}, sum = 0;
for (int i = 0; i < N; i++) cin >> a[i], sum += a[i];
sort(a, a + N);
cout << sum / N << "\n";
cout << a[2];
}
/*
대표값2(2587)
O(n log n)?
입력받음과 동시에 sum에 저장하고, a배열을 sort()
평균은 sum/N, 중앙값은 a[2]를 출력
*/
/* 2. selection sort를 이용한 풀이 O(N^2)*/
/*
#include <bits/stdc++.h>
#define N 5
using namespace std;
int main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
int a[N] = {}, sum = 0, m = 0;
for (int i = 0; i < N; i++) cin >> a[i], sum += a[i];
for (int i = 0; i < N; i++) {
int indexOfMin = i;
for (int j = i + 1; j < N; j++) {
if (a[indexOfMin] > a[j]) {
indexOfMin = j;
}
}
if (indexOfMin != i) {
int lesser = a[indexOfMin];
a[indexOfMin] = a[i];
a[i] = lesser;
}
}
cout << sum / N << "\n";
cout << a[2];
}
*/
// #include <bits/stdc++.h>
// using namespace std;
// int arr[5], sum;
// int main(void) {
// ios::sync_with_stdio(false), cin.tie(nullptr);
// for (int i = 0; i < 5; i++) cin >> arr[i];
// for (int i = 0; i < 5; i++) sum += arr[i];
// cout << sum / 5 << '\n';
// sort(arr, arr + 5);
// cout << arr[2];
// }
view raw 2587.cpp hosted with ❤ by GitHub
728x90
// Iterable keys
// importance: 5
// We’d like to get an array of map.keys() in a variable and then apply array-specific methods to it, e.g. .push.
// But that doesn’t work:
let map = new Map();
map.set("name", "John");
let keys = Array.from(map.keys());
// Error: keys.push is not a function
keys.push("more");
console.log(keys);
728x90
// Filter anagrams
// importance: 4
// Anagrams are words that have the same number of same letters, but in different order.
function aclean(arr) {
const map = new Map();
for(word of arr) {
const sorted = word.toLowerCase().split('').sort().join('');
map.set(sorted, word);
}
return Array.from(map.values()).join(', ');
}
let arr = ["nap", "teachers", "cheaters", "PAN", "ear", "era", "hectares"];
console.log( aclean(arr) ); // "nap,teachers,ear" or "PAN,cheaters,era"
// 2
// function aclean(arr) {
// const obj = {};
// for(let i = 0; i < arr.length; i++) {
// const sorted = arr[i].toLowerCase().split('').sort().join('');
// obj[sorted] = arr[i];
// }
// return Object.values(obj).join(', ');
// }
// let arr = ["nap", "teachers", "cheaters", "PAN", "ear", "era", "hectares"];
// console.log( aclean(arr) ); // "nap,teachers,ear" or "PAN,cheaters,era"
728x90
// Filter unique array members
// importance: 5
// Let arr be an array.
// Create a function unique(arr) that should return an array with unique items of arr.
function unique(arr) {
return Array.from(new Set(arr)).join(', ');
}
let values = ["Hare", "Krishna", "Hare", "Krishna",
"Krishna", "Krishna", "Hare", "Hare", ":-O"
];
console.log( unique(values) ); // Hare, Krishna, :-O
728x90

https://www.acmicpc.net/problem/2576

#include <bits/stdc++.h>
using namespace std;
int input, sumOdd, minValue = 100;
int main(void) {
ios::sync_with_stdio(false), cin.tie(nullptr);
for (int i = 0; i < 7; i++) {
cin >> input;
if (input % 2 != 0) {
sumOdd += input;
if (minValue > input) minValue = input;
}
}
if (minValue == 100)
cout << -1;
else
cout << sumOdd << '\n' << minValue;
}
// #include <bits/stdc++.h>
// #define N 7
// using namespace std;
// int main() {
// ios::sync_with_stdio(false), cin.tie(nullptr);
// int a, min = 101, sum = 0;
// for (int i = 0; i < N; i++) {
// cin >> a;
// if (a % 2 != 0) {
// sum += a;
// if (a < min) min = a;
// }
// }
// if (sum != 0) {
// cout << sum << "\n";
// cout << min;
// } else
// cout << -1;
// }
/*
홀수(2576)
O(N)
홀수일 경우 sum에 더하고, 최소값인지 확인하여 min에 저장
홀수가 없을 경우 sum == 0이므로, 0인지 확인 후 출력
마지막 풀이에서는 배열을 굳이 사용하지 않고, 입력받고 바로 확인하는 로직으로
변경
*/
/* 2. array b를 없애고 int min을 이용한 풀이(속도면에서 더 나은듯 하지만 역시
* 배열을 이용) */
/*
#include <bits/stdc++.h>
#define N 7
using namespace std;
int main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
int a[N];
int sum = 0, min = 101;
for (int i = 0; i < N; i++) cin >> a[i];
for (int i = 0; i < N; i++) {
if (a[i] % 2 != 0) {
sum += a[i];
if (a[i] < min) min = a[i];
}
}
if (sum != 0)
cout << sum << '\n', cout << min;
else
cout << -1;
}
*/
/* 1.배열을 이용한 풀이(공간면에서 굳이 배열을 이용할 필요가 없다) */
/*
#include <bits/stdc++.h>
#define N 7
using namespace std;
int main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
int a[N], b[N];
int sum = 0;
for (int i = 0; i < N; i++) cin >> a[i], b[i] = 101;
for (int i = 0; i < N; i++)
if (a[i] % 2 != 0) b[i] = a[i], sum += a[i];
sort(b, b + N);
if (sum != 0)
cout << sum << '\n', cout << b[0];
else
cout << -1;
}
*/
// Authored by : pha-ran (soft18)
// Co-authored by : -
// http://boj.kr/c2a05cf9d222441dba79bba4e9ac5b6e
// #include <bits/stdc++.h>
// using namespace std;
// int main(void) {
// ios::sync_with_stdio(0);
// cin.tie(0);
// int x, odd = 0, sum = 0, min = 100;
// for (int i = 0; i < 7; i++) {
// cin >> x;
// if (x & 1) {
// odd += 1;
// sum += x;
// if (x < min) {
// min = x;
// }
// }
// }
// if (odd)
// cout << sum << "\n" << min;
// else
// cout << "-1";
// }
view raw 2576.cpp hosted with ❤ by GitHub
728x90

https://www.acmicpc.net/problem/2562

#include <bits/stdc++.h>
#define N 9
using namespace std;
int main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
int indexOfMax = 0, a[N] = {};
for (int i = 0; i < N; i++) {
cin >> a[i];
if (a[indexOfMax] < a[i]) indexOfMax = i;
}
cout << a[indexOfMax] << '\n' << indexOfMax + 1;
}
/*
최댓값(2562)
O(N)
순차 검색을 이용해 최댓값 찾기
배열의 index를 0부터 시작하므로, 출력할 때 +1 해주기
*/
// #include <bits/stdc++.h>
// using namespace std;
// int input, maxValue, maxIndex;
// int main(void) {
// ios::sync_with_stdio(false), cin.tie(nullptr);
// for (int i = 1; i < 10; i++) {
// cin >> input;
// if (maxValue < input) {
// maxValue = input;
// maxIndex = i;
// }
// }
// cout << maxValue << '\n' << maxIndex;
// }
// Authored by : BaaaaaaaaaaarkingDog
// Co-authored by : -
// http://boj.kr/359583ea75ef49f7acf7c5564827ebba
// #include <bits/stdc++.h>
// using namespace std;
// int a[9];
// int main(void) {
// ios::sync_with_stdio(0);
// cin.tie(0);
// for (int i = 0; i < 9; i++) cin >> a[i];
// cout << *max_element(a, a + 9) << '\n';
// cout << max_element(a, a + 9) - a + 1;
// }
// /*
// max_element 함수를 이용한 풀이
// */
view raw 2562.cpp hosted with ❤ by GitHub
728x90

https://www.acmicpc.net/problem/2490

#include <bits/stdc++.h>
using namespace std;
int main(void) {
ios::sync_with_stdio(false), cin.tie(nullptr);
for (int i = 0; i < 3; i++) {
int num, sum = 0;
for (int j = 0; j < 4; j++) {
cin >> num, sum += num;
}
switch (sum) {
case 0:
cout << 'D'; // 윷
break;
case 1:
cout << 'C'; // 걸
break;
case 2:
cout << 'B'; // 개
break;
case 3:
cout << 'A'; // 도
break;
case 4:
cout << 'E'; // 모
break;
defalut:
break;
}
cout << '\n';
}
}
// Authored by : wogha95
// Co-authored by : BaaaaaaaaaaarkingDog
// http://boj.kr/38cf0450b8c8458e859c486e3ea80323
// #include <bits/stdc++.h>
// using namespace std;
// int result, input;
// string res = "DCBAE";
// int main(void) {
// ios::sync_with_stdio(0);
// cin.tie(0);
// for (int r = 0; r < 3; r++) {
// result = 0;
// for (int c = 0; c < 4; c++) {
// cin >> input;
// result += input;
// }
// cout << res[result] << '\n';
// }
// }
view raw 2490.cpp hosted with ❤ by GitHub

'Algorithm > C&C++' 카테고리의 다른 글

백준 2576번: 홀수(c/c++, c/cpp)  (0) 2021.10.05
백준 2562번: 최댓값(c/c++, c/cpp)  (0) 2021.10.05
백준 2480번: 주사위 세개(c/c++, c/cpp)  (0) 2021.09.29
백준 2753번: 윤년(c++)  (0) 2021.09.29
백준 1000번: A+B(c++)  (0) 2021.09.27
728x90

https://www.acmicpc.net/problem/2480

#include <bits/stdc++.h>
using namespace std;
int main(void) {
ios::sync_with_stdio(false), cin.tie(nullptr);
int a, b, c;
cin >> a >> b >> c;
if (a == b && b == c && a == c) {
cout << 10000 + a * 1000;
} else if (a == b) {
cout << 1000 + a * 100;
} else if (b == c) {
cout << 1000 + b * 100;
} else if (a == c) {
cout << 1000 + c * 100;
} else {
cout << max({a, b, c}) * 100;
}
}
// Authored by : BaaaaaaaaaaarkingDog
// Co-authored by : -
// http://boj.kr/0b6d46b39f7c4fc8afd6d0c7d21e7bad
// #include <bits/stdc++.h>
// using namespace std;
// int main(void) {
// ios::sync_with_stdio(0);
// cin.tie(0);
// int a, b, c;
// cin >> a >> b >> c;
// if (a == b && b == c)
// cout << 10000 + a * 1000;
// else if (a == b || a == c)
// cout << 1000 + a * 100;
// else if (b == c)
// cout << 1000 + b * 100;
// else
// cout << max({a, b, c}) * 100;
// }
// Authored by : BaaaaaaaaaaarkingDog
// Co-authored by : -
// http://boj.kr/a66f45063a2a4f1bb9816a2080077fe3
// #include <bits/stdc++.h>
// using namespace std;
// int main(void) {
// ios::sync_with_stdio(0);
// cin.tie(0);
// int a[3];
// cin >> a[0] >> a[1] >> a[2];
// sort(a, a + 3);
// if (a[0] == a[2])
// cout << 10000 + a[0] * 1000; // 세 수가 동일
// else if (a[0] == a[1] || a[1] == a[2])
// cout << 1000 + a[1] * 100; // 두 수가 동일
// else
// cout << a[2] * 100;
// }
// #include <bits/stdc++.h>
// using namespace std;
// int main() {
// ios::sync_with_stdio(false), cin.tie(nullptr);
// int a, b, c;
// cin >> a >> b >> c;
// if (a == b && b == c && c == a) {
// cout << 10000 + (a * 1000);
// } else if (a == b || a == c) {
// cout << 1000 + (a * 100);
// } else if (b == c) {
// cout << 1000 + (b * 100);
// } else {
// a > b ? (a > c ? cout << a * 100 : cout << c * 100) : (b > c ? cout << b
// * 100 : cout << c * 100);
// }
// }
/*
주사위(2480)
O(1)
3개의 눈이 같을 경우
(a == b && a == c)
2개의 눈이 같을 경우
(a == b) || (a == c) || (b == c)
같은 눈이 없을 경우 배열의 마지막 값을 이용하면 된다(sort 되어 있으므로)
*/
view raw bj_2480.cpp hosted with ❤ by GitHub

 

'Algorithm > C&C++' 카테고리의 다른 글

백준 2562번: 최댓값(c/c++, c/cpp)  (0) 2021.10.05
백준 2490번: 윷놀이(c/c++, c/cpp)  (0) 2021.10.05
백준 2753번: 윤년(c++)  (0) 2021.09.29
백준 1000번: A+B(c++)  (0) 2021.09.27
백준 2752번: 세수정렬 c++(cpp)  (0) 2021.09.06

+ Recent posts