#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
void func_1260_dfs_recursion(int n, vector<int> * graph, bool* visited) {
visited[n] = true;
cout << n << " ";
for (auto node : graph[n]) {
if (!visited[node]) {
func_1260_dfs_recursion(node, graph, visited);
}
}
}
void func_1260_bfs_queue(int n, vector<int> * graph, bool* visited) {
queue<int> q;
int now_node;
visited[n] = true;
q.push(n);
while (!q.empty()) {
now_node = q.front();
q.pop();
cout << now_node << " ";
for (auto node : graph[now_node]) {
if (!visited[node]) {
visited[node] = true;
q.push(node);
}
}
}
cout << "\n";
}
int main() {
int n, m, start_node, u, v;
cin >> n >> m >> start_node;
vector<int> *graph = new vector<int>[n+1];
bool *visited = new bool[n+1];
memset(visited, 0, sizeof(bool) * (n+1));
for (int i = 0; i < m; i++) {
cin >> u >> v;
graph[u].push_back(v);
graph[v].push_back(u);
}
for (int i = 1; i <= n; i++) {
sort(graph[i].begin(), graph[i].end());
}
func_1260_dfs_recursion(start_node, graph, visited);
cout << "\n";
memset(visited, 0, sizeof(bool) * (n + 1));
func_1260_bfs_queue(start_node, graph, visited);
return 0;
}
문제 2. 바이러스
#include <iostream>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;
static int func_2606_cnt;
void func_2606_dfs(int start, vector<int> *graph, bool* visited) {
visited[start] = true;
func_2606_cnt++;
for (auto node : graph[start]) {
if (!visited[node]) {
func_2606_dfs(node, graph, visited);
}
}
}
int main() {
int n, m, u, v;
cin >> n >> m;
vector<int> *graph = new vector<int>[n+1];
bool* visited = new bool[n+1];
memset(visited, 0, sizeof(bool) * (n+1));
func_2606_cnt = 0;
for (int i = 0; i < m; i++) {
cin >> u >> v;
graph[u].push_back(v);
graph[v].push_back(u);
}
for (int i = 1; i <= n; i++) {
sort(graph[i].begin(), graph[i].end());
}
func_2606_dfs(1, graph, visited);
cout << func_2606_cnt -1 << "\n";
return 0;
}
문제 3. 단지 번호 붙이기
#include<iostream>
#include<queue>
#include<vector>
#include<cstring>
#include<algorithm>
#include<utility>
using namespace std;
int func_2667_bfs(int cnt, int n, queue<pair<int, int>> q, int **map, int ** v) {
int _x, _y, a=0;
pair<int, int> now, tmp;
int** direc = new int* [4];
for (int i = 0; i < 4; i++) {
direc[i] = new int[2];
if (i == 0) {
_x = 0;
_y = -1;
}
else if (i == 1) {
_x = 1;
_y = 0;
}
else if (i == 2) {
_x = 0;
_y = 1;
}
else if (i == 3) {
_x = -1;
_y = 0;
}
direc[i][0] = _x;
direc[i][1] = _y;
}
while (!q.empty()) {
now = q.front();
q.pop();
if (v[now.first][now.second] != 1) {
v[now.first][now.second] = 1;
a++;
}
for (int i = 0; i < 4; i++) {
_x = now.first + direc[i][0];
_y = now.second + direc[i][1];
if (_x < 0 || _x >= n) {
continue;
}
if (_y < 0 || _y >= n) {
continue;
}
if (map[_x][_y] == 0) {
continue;
}
if (v[_x][_y] == 1) {
continue;
}
// 방문한적 없는 좌표인데 값이 1보다 크면 현재 카운트로 덮어쓰기 및 방문처리
if (map[_x][_y] >= 1) {
v[_x][_y] = 1;
map[_x][_y] = cnt;
q.push({_x, _y});
//cout << _x << ", " << _y << "\n";
a++;
}
}
}
for (int i = 0; i < 4; i++) {
delete direc[i];
}
return a;
}
int main() {
int n, cnt =0;
string str;
queue<pair<int, int>> q;
vector<int> m;
int tmp_m;
cin >> n;
int** v = new int* [n];
for (int i = 0; i < n; i++) {
v[i] = new int[n];
for (int j = 0; j < n; j++) {
v[i][j] = 0;
}
}
int** _map = new int* [n];
for (int i = 0; i < n; i++) {
str = "";
_map[i] = new int[n];
cin >> str;
for (int j = 0; j < n; j++) {
_map[i][j] = str[j] - '0';
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (_map[i][j] == 1) {
q.push({ i, j });
tmp_m = func_2667_bfs(cnt+1, n, q, _map, v);
if (tmp_m != 0) {
m.push_back(tmp_m);
cnt++;
}
}
}
}
sort(m.begin(), m.end());
cout << cnt << "\n";
for (int i = 0; i < cnt; i++) {
cout << m[i] << "\n";
}
return 0;
}
문제 4. 유기농 배추
#include <iostream>
#include <utility>
#include <cstring>
#include <queue>
using namespace std;
int main() {
int test_case;
int n, m, k;
int u, v;
int _x, _y;
pair<int, int> dir[4] = { {0, 1}, {1, 0}, {0, -1}, {-1, 0} }; // 상, 우, 하, 좌
pair<int, int> tmp_q;
int graph_count = 0;
int** _map;
int** _visit;
queue<pair<int, int>> q;
cin >> test_case;
for (int a = 0; a < test_case; a++) {
cin >> m >> n >> k;
// Map 생성 및 초기화
_map = new int* [n];
_visit = new int* [n];
for (int i = 0; i < n; i++) {
_map[i] = new int[m];
_visit[i] = new int[m];
memset(_map[i], 0, sizeof(int) * m);
memset(_visit[i], 0, sizeof(int) * m);
}
// Map 업데이트
for (int i = 0; i < k; i++) {
cin >> u >> v;
_map[v][u] = 1;
}
graph_count = 0;
// Map 탐색
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
// 지도에서 현재 좌표가 1이고 방문한 적이 없으면 현재 좌표를 q에 등록하고 방문 처리
if (_map[i][j] == 1 && _visit[i][j] != 1) {
q.push({i, j});
_visit[i][j] = 1;
graph_count++;
}
// q에 값이 있으면
while (!q.empty()) {
tmp_q = q.front();
q.pop();
// 현재 좌표에서 인접한 4방향 좌표를 검사, 위부터 시계방향
for (int i = 0; i < 4; i++) {
_x = tmp_q.first + dir[i].first;
_y = tmp_q.second + dir[i].second;
// 경계선 검사
if (_x >= n || _x < 0)
continue;
if (_y >= m || _y < 0)
continue;
// 값 검사
if (_map[_x][_y] == 0)
continue;
// 방문여부 검사
if (_visit[_x][_y] == 1)
continue;
// 방문기록 남기고 다음 작업대상에 추가
_visit[_x][_y] = 1;
q.push({_x, _y});
}
}
}
}
cout << graph_count << "\n";
// 메모리 해제
for (int i = 0; i < n; i++) {
free(_map[i]);
free(_visit[i]);
}
}
return 0;
}
문제 5. 미로 탐색
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
// 노드 정보 저장용 클래스
class func_2178_node {
public:
int x;
int y;
int value;
int visited;
};
int func_2178_BFS(int n, int m, func_2178_node **_map, int *start_node, int *end_node) {
// BFS 탐색을 위한 큐 생성 현재 노드 저장
queue<func_2178_node> q;
func_2178_node tmp_node, now_node;
// 상0, 우1, 하2, 좌3
int** direction = new int*[4];
for (int i = 0; i < 4; i++) {
direction[i] = new int[2];
if (i == 0) {
direction[i][0] = 0;
direction[i][1] = -1;
}
if (i == 1) {
direction[i][0] = 1;
direction[i][1] = 0;
}
if (i == 2) {
direction[i][0] = 0;
direction[i][1] = 1;
}
if (i == 3) {
direction[i][0] = -1;
direction[i][1] = 0;
}
}
// 시작노드의 방문 여부 변경
_map[start_node[0]][start_node[1]].visited = 1;
// 시작노드 넣기
q.push(_map[start_node[0]][start_node[1]]);
// 큐에 값이 있으면 탐색 시작
while (!q.empty()) {
// 현재 큐의 값 가져오기 + pop
now_node = q.front();
q.pop();
// 인접 노드 탐색 (시계방향으로 탐색)
for (int i = 0; i < 4; i++) {
// 현재 좌표가 좌우를 넘어가는지 확인
if (now_node.x + direction[i][0] <= 0 || now_node.x + direction[i][0] > n) {
continue;
}
// 현재 좌표가 상하를 넘어가는지 확인
if (now_node.y + direction[i][1] <= 0 || now_node.y + direction[i][1] > m) {
continue;
}
// 이동한 값이 맵 안에 있으면 노드 이동
tmp_node = _map[now_node.x + direction[i][0]][now_node.y + direction[i][1]];
// 값이 1이 아닌지 검사, 아니면 continue;
if (tmp_node.value == 0) {
continue;
}
// 방문 여부가 없는지 검사 (방문했으면 다음거)
if (tmp_node.visited) {
continue;
}
_map[tmp_node.x][tmp_node.y].visited = _map[now_node.x][now_node.y].visited + 1;
q.push(_map[tmp_node.x][tmp_node.y]);
}
}
return _map[n][m].visited;
}
int main() {
int n, m;
int* start_node = new int[2];
int* end_node = new int[2];
string str_input;
cin >> n >> m;
start_node[0] = 1;
start_node[1] = 1;
end_node[0] = n;
end_node[1] = m;
func_2178_node** map = new func_2178_node*[n+1];
for (int i = 1; i <= n; i++) {
map[i] = new func_2178_node[m+1];
}
for (int i = 1; i <= n; i++) {
cin >> str_input;
for (int j = 1; j <= m; j++) {
map[i][j].x = i;
map[i][j].y = j;
map[i][j].value = str_input[j-1] - '0';
map[i][j].visited = 0;
}
}
cout << func_2178_BFS(n, m, map, start_node, end_node) << "\n";
return 0;
}
문제 6. 토마토
#include <iostream>
#include <queue>
using namespace std;
class func_7576_tomato {
public:
int x;
int y;
int visited;
int value;
};
void func_7576_bfs(int n, int m, func_7576_tomato** box, queue<func_7576_tomato> q) {
func_7576_tomato now_tomato, tmp_tomato;
// 방향 정의 (시계방향)
int** direc = new int*[4];
int _x, _y;
for (int i = 0; i < 4; i++) {
direc[i] = new int[2];
// 상
if (i == 0) {
_x = 0;
_y = -1;
}
// 우
if (i == 1) {
_x = 1;
_y = 0;
}
// 하
if (i == 2) {
_x = 0;
_y = 1;
}
// 좌
if (i == 3) {
_x = -1;
_y = 0;
}
direc[i][0] = _x;
direc[i][1] = _y;
}
// bfs 탐색 시작
while (!q.empty ()) {
now_tomato = q.front();
q.pop();
// 각 방향별 탐색
for (int i = 0; i < 4; i++) {
// 이동한 범위가 토마토 상자 내부인지 검사 (x축)
if (now_tomato.x + direc[i][0] < 0 || now_tomato.x + direc[i][0] >= n) {
continue;
}
// 이동한 범위가 토마토 상자 내부인지 검사 (y축)
if (now_tomato.y + direc[i][1] < 0 || now_tomato.y + direc[i][1] >= m) {
continue;
}
tmp_tomato = box[now_tomato.x + direc[i][0]][now_tomato.y + direc[i][1]];
// 해당 위치가 벽인지 검사
if (tmp_tomato.value != 0) {
continue;
}
// 해당 위치에 숙성되지 않은 토마토가 있으면 숙성 시키고 몇 일차에 숙성시켰는지 입력 후 큐에 등록
box[tmp_tomato.x][tmp_tomato.y].value = 1;
box[tmp_tomato.x][tmp_tomato.y].visited = now_tomato.visited + 1;
q.push(box[tmp_tomato.x][tmp_tomato.y]);
}
}
}
int main() {
int m, n, tmp, max;
queue<func_7576_tomato> q;
// 노드 개수
cin >> m >> n;
// 노드 생성
func_7576_tomato** box = new func_7576_tomato*[n];
for (int i = 0; i < n; i++) {
box[i] = new func_7576_tomato[m];
}
// 초기화
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
box[i][j].x = i;
box[i][j].y = j;
box[i][j].visited = 0;
box[i][j].value = 0;
}
}
// 입력값 받기, 입력에 따른 값 설정
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> tmp;
box[i][j].value = tmp;
//입력 값이 1이면 큐에 등록
if (tmp == 1) {
q.push(box[i][j]);
}
}
}
func_7576_bfs(n, m, box, q);
max = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
// 숙성안된 토마토가 있으면 -1
if (box[i][j].value == 0) {
max = -1;
break;
}
// 최대값 찾아서 갱신
if (max < box[i][j].visited) {
max = box[i][j].visited;
}
}
if (max == -1) {
break;
}
}
cout << max << "\n";
return 0;
}
문제 7. 토마토
문제 8. 숨바꼭질
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
void func_1697_bfs(int k, queue<int> q, int *visited) {
int now;
while (!q.empty()) {
now = q.front();
q.pop();
if (now == k) {
break;
}
if (visited[now - 1] == 0 && (now - 1) >= 0) {
visited[now - 1] = visited[now] + 1;
q.push(now-1);
}
if ( (now+1) < 100000 && visited[now + 1] == 0) {
visited[now + 1] = visited[now] + 1;
q.push(now+1);
}
if ( (now * 2) <= 100000 && visited[now * 2] == 0) {
visited[now * 2] = visited[now] + 1;
q.push(now * 2);
}
}
}
int main() {
int n, k;
cin >> n >> k;
int* visited = new int[100000];
memset(visited, 0, sizeof(int) * 100000);
queue<int> q;
q.push(n);
visited[n] = 1;
func_1697_bfs(k, q, visited);
cout << visited[k]-1 << "\n";
return 0;
}
#include<iostream>
using namespace std;
int main(){
int n, m, sum, min, res = 0;
cin >> n >> m;
int* card = new int[n];
bool is_flag;
for (int i = 0; i < n; i++) {
cin >> card[i];
}
sum = 0;
min = m;
is_flag = true;
for (int i = 0; i < n - 1 && is_flag; i++) {
for (int j = i + 1; j < n && is_flag; j++) {
for (int k = j + 1; k < n && is_flag; k++) {
sum = card[i] + card[j] + card[k];
if (sum == m) is_flag = false;
if (min > m - sum && m >= sum) {
min = m - sum;
res = sum;
}
}
}
}
cout << res << endl;
return 0;
}
문제 2. 분해합 (브론즈 2)
#include <iostream>
#include <string>
#include <cmath>
#include <map>
using namespace std;
int func_2231_create_decompose(int a) {
int len, res = a;
len = to_string(a).length();
for (int i = len; a > 0; i--) {
res += a / pow(10, i);
a %= (int)pow(10, i);
}
return res;
}
int main() {
int a, key;
cin >> a;
map<int, int> m;
for (int i = 1; i < a; i++) {
key = func_2231_create_decompose(i);
if(!m[key])
m[key] = i;
}
cout << m[a];
return 0;
}
문제 3. 덩치 (실버 5)
/*
* 문제 내용 이해가 중요한 문제였음.
* map을 사용해서 구현한 버전
*/
#include <iostream>
#include <map>
using namespace std;
int main() {
int int_case;
int cnt, grade;
int* bulk;
cin >> int_case;
map<char, int *> m_input;
// 입력
for (int i = 0; i < int_case; i++) {
bulk = new int[2];
cin >> bulk[0] >> bulk[1];
m_input['a' + i] = bulk;
}
// 등수 : 나보다 덩치가 큰 사람의 수 + 1
for (int i = 0; i < int_case; i++) {
cnt = 1;
for (int j = 0; j < int_case; j++) {
// 덩치 : 키도 크고, 몸무게도 무거운 사람 수
if (m_input['a' + i][0] < m_input['a' + j][0] && m_input['a' + i][1] < m_input['a' + j][1]) {
cnt++;
}
}
// 등수 출력
cout << cnt << " ";
}
for (int i = 0; i < int_case; i++) {
delete m_input['a' + i];
}
return 0;
}
/*
* 문제 내용 이해가 중요한 문제였음.
* 2차원 Array를 사용해서 구현한 버전
*/
#include <iostream>
using namespace std;
int main() {
int int_case, cnt;
cin >> int_case;
int** arr = new int*[int_case];
int* bulk;
for (int i = 0; i < int_case; i++) {
bulk = new int[2];
cin >> bulk[0] >> bulk[1];
arr[i] = bulk;
}
for (int i = 0; i < int_case; i++) {
cnt = 1;
for (int j = 0; j < int_case; j++) {
if (arr[i][0] < arr[j][0] && arr[i][1] < arr[j][1]) {
cnt++;
}
}
cout << cnt << " ";
}
return 0;
}
문제 4. 체스판 다시 칠하기 (실버 5)
#include <iostream>
#include <cstring>
using namespace std;
int func_1018_bitmask(char **_map) {
int cnt_1 = 0, cnt_2 = 0;
char filter_1[8][8] = {
{0, 1, 0, 1, 0, 1, 0, 1},
{1, 0, 1, 0, 1, 0, 1, 0},
{0, 1, 0, 1, 0, 1, 0, 1},
{1, 0, 1, 0, 1, 0, 1, 0},
{0, 1, 0, 1, 0, 1, 0, 1},
{1, 0, 1, 0, 1, 0, 1, 0},
{0, 1, 0, 1, 0, 1, 0, 1},
{1, 0, 1, 0, 1, 0, 1, 0}
};
char filter_2[8][8] = {
{1, 0, 1, 0, 1, 0, 1, 0},
{0, 1, 0, 1, 0, 1, 0, 1},
{1, 0, 1, 0, 1, 0, 1, 0},
{0, 1, 0, 1, 0, 1, 0, 1},
{1, 0, 1, 0, 1, 0, 1, 0},
{0, 1, 0, 1, 0, 1, 0, 1},
{1, 0, 1, 0, 1, 0, 1, 0},
{0, 1, 0, 1, 0, 1, 0, 1}
};
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if ( !(_map[i][j]^filter_1[i][j]) ) {
cnt_1++;
}
if (!(_map[i][j] ^ filter_2[i][j])) {
cnt_2++;
}
}
}
if (cnt_1 >= cnt_2) return cnt_2;
else return cnt_1;
}
int main() {
int n, m;
int min, cnt;
string tmp;
cin >> n >> m;
char** tmp_map = new char* [8];
for (int i = 0; i < 8; i++) {
tmp_map[i] = new char[8];
}
char** bwmap = new char* [n];
for (int i = 0; i < n; i++) {
bwmap[i] = new char[m];
}
for (int i = 0; i < n; i++) {
cin >> tmp;
for (int j = 0; j < m; j++) {
if (tmp[j] == 'B') {
bwmap[i][j] = 1;
}
else {
bwmap[i][j] = 0;
}
}
}
cnt = 0;
min = 64;
for (int i = 0; i <= n - 8; i++) {
for (int j = 0; j <= m - 8; j++) {
for (int a = i, aa = 0; a < i + 8; a++, aa++) {
for (int b = j, bb = 0; b < j + 8; b++, bb++) {
tmp_map[aa][bb] = bwmap[a][b];
}
}
cnt = func_1018_bitmask(tmp_map);
if (cnt < min) {
min = cnt;
}
}
}
cout << min << "\n";
return 0;
}
문제 5. 영화감독 숌 (실버 5)
#include <iostream>
#include <cstring>
using namespace std;
int main() {
int n, cnt=0;
string tmp;
cin >> n;
for (int i = 666; i < 2100000000; i++) {
tmp = to_string(i);
if (tmp.find("666") != string::npos) {
cnt++;
if (cnt > 10000) break;
if (cnt == n) {
cout << tmp << "\n";
}
}
}
}
#include<iostream>
typedef unsigned long long ll;
using namespace std;
int is_prime_number_custom(ll input)
{
if (input < 2) {
return 0;
}
for (ll j = 2; j <= (ll)(input / j); j++)
{
if (input % j == 0)
{
return 0;
}
}
return 1;
}
int main() {
int a, cnt=0;
cin >> a;
int* arr = new int[a];
for (int i = 0; i < a; i++) {
cin >> arr[i];
if (is_prime_number_custom(arr[i])) {
cnt++;
}
}
cout << cnt;
return 0;
}
문제 2. 소수
#include<iostream>
typedef unsigned long long ll;
using namespace std;
int is_prime_number_custom(ll input)
{
if (input < 2) {
return 0;
}
for (ll j = 2; j <= (ll)(input / j); j++)
{
if (input % j == 0)
{
return 0;
}
}
return 1;
}
int main() {
int a, b, min, sum = 0;
cin >> a >> b;
for (int i = a; i <= b; i++) {
if (is_prime_number_custom(i)) {
if (sum == 0) min = i;
sum+=i;
}
}
if (sum == 0) cout << -1 << endl;
else {
cout << sum << endl;
cout << min;
}
return 0;
}
문제 3. 소인수분해
#include<iostream>
using namespace std;
void func_11653_Factorization(int a) {
bool is_flag = true;
for (int i = 2; i < a; i++) {
if (a % i == 0) {
is_flag = false;
cout << i << endl;
func_11653_Factorization(a / i);
break;
}
}
if(is_flag) cout << a << endl;
}
int main() {
int a;
cin >> a;
if (a != 1) {
func_11653_Factorization(a);
}
return 0;
}
문제 4. 소수 구하기
#include<stdio.h>
#include<math.h>
typedef unsigned long long ll;
int is_prime_number_custom(ll input)
{
if (input < 2) {
return 0;
}
for (ll j = 2; j <= (ll)(input / j); j++)
{
if (input % j == 0)
{
return 0;
}
}
return 1;
}
int main() {
int int_M, int_N;
scanf("%d %d", &int_M, &int_N);
for(int i = int_M; i <= int_N; i++)
{
if(is_prime_number_custom(i)){
printf("%d\n", i);
}
}
return 0;
}
문제 5. 베르트랑 공준
#include<iostream>
#include <cstring>
using namespace std;
bool *Sieve_of_Eratosthenes(int m) {
bool* arr = new bool[m + 1];
memset(arr, 1, sizeof(bool) * (m+1));
arr[0] = false;
arr[0] = false;
for (int i = 2; i < m + 1; i++) {
if (arr[i] == true) {
for (int j = i * 2; j < m + 1; j += i) {
arr[j] = false;
}
}
}
return arr;
}
int main() {
int int_N, cnt;
bool* arr = Sieve_of_Eratosthenes(123456 * 2);
do {
cin >> int_N;
cnt = 0;
for (int i = int_N + 1; i <= int_N*2; i++)
{
if (arr[i]) {
cnt++;
}
}
if(int_N != 0) cout << cnt << endl;
} while (int_N != 0);
return 0;
}
문제 6. 골드바흐의 추측
#include<iostream>
#include<cstring>
#include<vector>
using namespace std;
bool *Sieve_of_Eratosthenes(int m) {
bool* arr = new bool[m + 1];
memset(arr, 1, sizeof(bool) * (m+1));
arr[0] = false;
arr[0] = false;
for (int i = 2; i < m + 1; i++) {
if (arr[i] == true) {
for (int j = i * 2; j < m + 1; j += i) {
arr[j] = false;
}
}
}
return arr;
}
int main() {
int int_a, int_N, int_sum;
bool* arr = Sieve_of_Eratosthenes(20000);
int tmp_a, tmp_b, tmp_min;
vector<int> v;
cin >> int_a;
for (int i = 0; i < int_a; i++) {
cin >> int_N;
int_sum = 0;
v.clear();
for (int j = 2; j < int_N; j++) {
if (arr[j]) {
v.push_back(j);
}
}
tmp_a = 0;
tmp_b = 0;
tmp_min = int_N;
for (int j = 0; j < v.size(); j++) {
for (int k = 0; k < v.size(); k++) {
if (int_N == v.at(j) + v.at(k) && tmp_min > abs(v.at(j) - v.at(k))) {
tmp_a = v.at(j);
tmp_b = v.at(k);
tmp_min = abs(v.at(j) - v.at(k));
}
}
}
if (tmp_a + tmp_b != 0) cout << tmp_a << " " << tmp_b << endl;
}
return 0;
}
문제 7. 직사각형에서 탈출
#include<iostream>
using namespace std;
int main() {
int x, y, w, h, min;
cin >> x;
min = x;
cin >> y;
if (min > y) min = y;
cin >> w;
if (min > w-x) min = w-x;
cin >> h;
if (min > h - y) min = h - y;
cout << min;
return 0;
}
문제 8. 네 번째 점
#include<iostream>
#include<map>
using namespace std;
int main() {
int a, b;
map<int, int> x, y;
map<int, int>::iterator iter;
for (int i = 0; i < 3; i++) {
cin >> a >> b;
if (x.count(a)) x[a]++;
else x[a] = 1;
if (y.count(b)) y[b]++;
else y[b]=1;
}
for (iter = x.begin(); iter != x.end(); iter++) {
if (iter->second == 1) {
cout << iter->first << " ";
}
}
for (iter = y.begin(); iter != y.end(); iter++) {
if (iter->second == 1) {
cout << iter->first;
}
}
return 0;
}
문제 9. 직각삼각형
#include<iostream>
#include<cmath>
using namespace std;
int main() {
int a, b, c;
int pow_a, pow_b, pow_c, pow_sum;
while (1) {
cin >> a >> b >> c;
pow_a = pow(a, 2);
pow_b = pow(b, 2);
pow_c = pow(c, 2);
pow_sum = pow_a + pow_b + pow_c;
if (pow_sum == 0)
break;
if (pow_a == pow_sum - pow_a || pow_b == pow_sum - pow_b || pow_c == pow_sum - pow_c) {
cout << "right" << endl;
}
else {
cout << "wrong" << endl;
}
}
return 0;
}
#include <iostream>
using namespace std;
int main() {
int a, b, c;
cin >> a >> b >> c;
int unit_profit = c - b;
if (unit_profit <= 0) {
cout << -1;
}
else {
cout << a / unit_profit +1 << endl;
}
return 0;
}
/*
* 반복으로 문제를 풀 경우 시간 초과 발생
*/
문제 2. 벌집
#include <iostream>
using namespace std;
int main() {
int a;
cin >> a;
for (int i = 1; true; i++) {
if (a == 1) {
cout << i;
break;
}
a -= (6 * i);
if (a <= 0) {
cout << i+1;
break;
}
}
return 0;
}
/*
* 각 껍질당 1 -> 6 -> 12 -> 18 ... 로 6 * i 형태로 올라감
* 입력 받은 수에서 6 * i 씩 감소시켜 몇 칸을 지나는지 알수있음
* , 1일 경우 출력 후 바로 종료
*/
문제 3. 분수찾기
#include <iostream>
using namespace std;
unsigned long long func_1193_sum_of_arithmetic_sequences(int n) {
return ( n * (n+1) ) / 2;
}
string func_1193_create(int n) {
// 입력받은 인덱스를 규칙을 기반으로 생성 (입력시마다 생성, 모든 수가 비슷한 시간)
// 규칙. 칸 수가 내려갈수록 1개씩 증가 1-> 2 -> 3 -> 4 ... 그리고 a/b 가 a는 증가, b는 감소
int lv, pos;
int a, b;
// 계층 구하기
for (int i = 0; i < 10000; i++) {
if (n <= func_1193_sum_of_arithmetic_sequences(i)) {
lv = i;
break;
}
}
// 계층과 차이나는 위치값 구하기
pos = n - func_1193_sum_of_arithmetic_sequences(lv);
// 시작지점의 값 설정
if (lv % 2 == 0) {
a = 1;
b = lv;
pos *= -1;
}
else {
a = lv;
b = 1;
}
// 차이나는 값 만큼 보정
a += pos;
b -= pos;
return to_string(b) + "/" + to_string(a);
}
int main() {
int a;
cin >> a;
cout << func_1193_create(a) << endl;
return 0;
}
문제 4. 달팽이는 올라가고 싶다
#include <iostream>
using namespace std;
int main() {
long long a, b, v, day;
cin >> a >> b >> v;
if (v <= a) {
day = 1;
}
else {
day = (v - b) / (a - b);
if ((v - b) % (a - b) > 0) {
day++;
}
}
cout << day << endl;
return 0;
}
문제 5. ACM 호텔
#include <iostream>
using namespace std;
int main() {
int a, h, w, n;
int f, no;
cin >> a;
for (int i = 0; i < a; i++) {
f = 1;
no = 1;
cin >> h >> w >> n;
f += (n % h) -1;
if (n % h == 0) {
f = h;
no = n / h;
}
else {
no += (n / h);
}
printf("%d%02d\n", f , no);
}
}
문제 6. 부녀회장이 될테야
#include <iostream>
using namespace std;
int func_2775_sum_people(int k, int n) {
if (k == 0) return n;
if (n == 1) return 1;
return func_2775_sum_people(k, n - 1) + func_2775_sum_people(k-1, n);
}
int main() {
int int_k, int_n, int_t;
cin >> int_t;
for (int i = 0; i < int_t; i++) {
cin >> int_k;
cin >> int_n;
cout << func_2775_sum_people(int_k, int_n) << endl;
}
return 0;
}
문제 7. 설탕배달
#include <iostream>
using namespace std;
int main() {
int a, b, n, min, tmp, res;
bool is_flag;
cin >> n;
min = n;
res = -1;
is_flag = false;
for (int a = 0; a < n; a++) {
for (int b = 0; b < n; b++) {
tmp = (3 * a) + (5 * b);
if (tmp > n) break;
if (tmp == n) {
if (min >= a + b) {
min = a + b;
is_flag = true;
}
}
}
}
if (is_flag) res = min;
cout << res << endl;
}
문제 8. 큰수 A+B
#include <iostream>
#include <vector>
using namespace std;
vector<int> func_10757_reverseVector(string num) {
vector<int> v;
for (int i = num.length() - 1; i >= 0; i--) {
v.push_back( (num[i] - '0'));
}
return v;
}
int main() {
string str_a, str_b;
cin >> str_a >> str_b;
vector<int> a, b, c;
int int_a, int_b, int_tmp = 0, int_len;
a = func_10757_reverseVector(str_a);
b = func_10757_reverseVector(str_b);
if (a.size() >= b.size()) {
int_len = a.size();
}
else {
int_len = b.size();
}
for (int i = 0; i < int_len; i++) {
try {
int_a = a.at(i);
}
catch (exception e) {
int_a = 0;
}
try {
int_b = b.at(i);
}
catch (exception e) {
int_b = 0;
}
c.push_back((int_a + int_b + int_tmp) % 10);
if ((int_a + int_b + int_tmp) >= 10) {
int_tmp = 1;
}
else {
int_tmp = 0;
}
}
if (int_tmp) {
c.push_back(1);
}
for (int i = c.size() - 1; i >= 0; i--) {
cout << c.at(i);
}
return 0;
}
문제 9. Fly me to the Alpha Centauri
#include <iostream>
#include <cmath>
using namespace std;
typedef unsigned long long ll;
int main() {
int int_case;
ll x, y;
ll res;
ll distance, level;
cin >> int_case;
for (int i = 0; i < int_case; i++) {
cin >> x >> y;
distance = y - x;
if (distance > 0 && distance <= 3) {
res = distance;
}
else {
level = (int)sqrt(distance);
if (((level + 1) * (level + 1)) - (level+1) < distance && distance < ((level + 1) * (level + 1))) {
res = 2 * level+1;
}
else if (distance == level * level ) {
res = 2 * level -1;
}
else {
res = 2 * level;
}
}
cout << res << "\n";
}
return 0;
}
#include <vector>
using namespace std;
long long sum(vector<int>& a) {
long long ans = 0;
for (int i = 0; i < a.size(); i++) {
ans += a.at(i);
}
return ans;
}
문제 2. 셀프 넘버
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int selfnumber(int a) {
int digit = to_string(a).size(), tmp = 0, a_copy = a, sum = a;
int* arr = new int[digit];
for (int i = 0; i < digit; i++) {
tmp = pow(10, (digit - (i + 1)));
arr[i] = a_copy / tmp;
a_copy %= tmp;
}
for (int i = 0; i < digit; i++) {
sum += arr[i];
}
return sum;
}
int main() {
int a = 10000;
cin.tie(NULL);
cin.sync_with_stdio(false);
int* arr = new int[a];
for (int i = 0; i < a; i++) {
arr[i] = 0;
}
for (int i = 0; i < a; i++) {
arr[selfnumber(i)-1]++;
}
for (int i = 0; i < a; i++) {
if (arr[i] == 0) {
cout << i+1 << endl;
}
}
return 0;
}
문제 3. 한수
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
bool hansu(int a) {
bool is_flag = false;
int tmp, cnt = 0;
int digit = to_string(a).size();
if (digit > 2) {
int* arr = new int[digit];
for (int i = 0; i < digit; i++) {
tmp = pow(10, (digit - (i + 1)));
arr[i] = a / tmp;
a %= tmp;
}
int now_dif = 0;
int pre_dif = arr[0] - arr[1];
for (int i = 0; i < digit - 1; i++) {
now_dif = arr[i] - arr[i + 1];
if (now_dif == pre_dif) cnt++;
pre_dif = now_dif;
}
if (digit - 1 == cnt) {
is_flag = true;
}
}
else {
is_flag = true;
}
return is_flag;
}
int main() {
int a = 0, cnt = 0;
cin.tie(NULL);
cin.sync_with_stdio(false);
cin >> a;
for (int i = 1; i <= a; i++) {
if (hansu(i)) cnt++;
}
cout << cnt << endl;
return 0;
}
#include <iostream>
#include <vector>
using namespace std;
int min(vector<int>& arr) {
int min = arr.front();
for (int i = 0; i < arr.size(); i++) {
if (arr.at(i) < min) {
min = arr.at(i);
}
}
return min;
}
int max(vector<int>& arr) {
int max = arr.front();
for (int i = 0; i < arr.size(); i++) {
if (arr.at(i) > max) {
max = arr.at(i);
}
}
return max;
}
int main() {
int a, tmp;
vector<int> arr;
cin >> a;
for (int i = 0; i < a; i++){
cin >> tmp;
arr.push_back(tmp);
}
cout << min(arr) << " " << max(arr);
return 0;
}
문제 2. 최댓값
#include <iostream>
#include <vector>
using namespace std;
int main() {
int tmp, max;
vector<int> arr;
for (int i = 0; i < 9; i++){
cin >> tmp;
arr.push_back(tmp);
}
max = arr.front();
for (int i = 0; i < arr.size(); i++) {
if (arr.at(i) >= max) {
max = arr.at(i);
tmp = i+1;
}
}
cout << max << endl;
cout << tmp;
return 0;
}
문제 3. 숫자의 개수
#include <iostream>
#include <string>
using namespace std;
int main() {
int a, b, c;
int arr[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
string str_abc;
cin >> a >> b >> c;
str_abc = to_string(a * b * c);
for (int i = 0; i < str_abc.length(); i++) {
arr[int(char(str_abc[i])) - int('0')]++;
}
for (int i = 0; i < 10; i++) {
cout << arr[i] << endl;
}
return 0;
}
문제 4. 나머지
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> arr;
int a;
for (int i = 0; i < 10; i++) {
cin >> a;
arr.push_back(a % 42);
}
sort(arr.begin(), arr.end());
arr.erase(unique(arr.begin(), arr.end()), arr.end());
cout << arr.size() << endl;
return 0;
}
문제 5. 평균
#include <iostream>
using namespace std;
int main() {
int a, max;
double aver=0;
cin >> a;
int* arr = new int[a];
double* arr2 = new double[a];
for (int i = 0; i < a; i++) {
cin >> arr[i];
if (i == 0) {
max = arr[0];
}
if (arr[i] > max) {
max = arr[i];
}
}
for (int i = 0; i < a; i++) {
arr2[i] = double(arr[i]) / max * 100;
}
for (int i = 0; i < a; i++) {
aver += arr2[i];
}
cout.precision(6);
cout << aver / a << endl;
return 0;
}
문제 6. OX퀴즈
#include <iostream>
using namespace std;
int point(string str) {
bool is_flag = false;
int cnt = 1, sum = 0;
for (int i = 0; i <= str.size(); i++) {
if (str[i] == 'O') {
if(is_flag) cnt++;
is_flag = true;
sum += cnt;
}
else {
cnt = 1;
is_flag = false;
}
}
return sum;
}
int main() {
int a;
cin >> a;
int* arr = new int[a];
string* str_arr = new string[a];
for (int i = 0; i < a; i++) {
cin >> str_arr[i];
}
for (int i = 0; i < a; i++) {
cout << point(str_arr[i]) << endl;
}
return 0;
}
문제 7. 평균은 넘겠지
#include <iostream>
#include <vector>
#include <stdio.h>
using namespace std;
int main() {
int a, b, sum, cnt, all;
double aver;
cin >> a;
vector<int *> vec;
char str_tmp[100];
for (int i = 0; i < a; i++) {
cin >> b;
int* arr = new int[b + 1];
arr[0] = b;
for (int j = 1; j <= b; j++) {
cin >> arr[j];
}
vec.push_back(arr);
}
for (int i = 0; i < vec.size(); i++) {
all = int(vec.at(i)[0]);
sum = 0;
for (int j = 1; j <= all; j++) {
sum += vec.at(i)[j];
}
aver = double(sum) / all;
cnt = 0;
for (int j = 1; j <= all; j++) {
if (aver < vec.at(i)[j]) {
cnt += 1;
}
}
sprintf(str_tmp, "%.3f", double(cnt) / all * 100);
cout << str_tmp << "%" << endl;
}
return 0;
}
#include <iostream>
using namespace std;
int main() {
int a, b;
while (true) {
cin >> a >> b;
if (a + b == 0) {
break;
}
cout << a + b << endl;
}
return 0;
}
문제 2. A+B - 4
#include <iostream>
using namespace std;
int main() {
int a, b;
while (cin >> a >> b) {
cout << a + b << endl;
}
return 0;
}
문제 3. 더하기 사이클
#include <iostream>
using namespace std;
void func(int a, int *front, int *back) {
if (a < 10) {
if(front) *front = 0;
if(back) *back = a;
}
else {
if (front) *front = a / 10;
if (back) *back = a % 10;
}
}
int main() {
int a, b, cycle = 0;
int front_a, back_a, back_b;
cin >> a;
b = a;
do {
func(b, &front_a, &back_a);
func(front_a + back_a, NULL, &back_b);
b = back_a * 10 + back_b;
cycle++;
} while (a != b);
cout << cycle;
return 0;
}
#include <iostream>
using namespace std;
int main() {
int a;
cin >> a;
for (int i=1; i<10; i++) {
cout << a << " * " << i << " = " << a * i << endl;
}
return 0;
}
문제 2. A+B - 3
#include <iostream>
using namespace std;
int main() {
int cnt=0, a, b;
cin >> cnt;
int *arr = new int[cnt];
for (int i = 0; i < cnt; i++) {
cin >> a >> b;
arr[i] = a + b;
}
for (int i = 0; i < cnt; i++) {
cout << arr[i] << endl;
}
return 0;
}
문제 3. 합
#include <iostream>
using namespace std;
int main() {
int a, sum=0;
cin >> a;
for (int i = 1; i <= a; i++) {
sum += i;
}
cout << sum;
return 0;
}
문제 4. 빠른 A+B
#include <iostream>
using namespace std;
int main() {
int cnt = 0, a, b;
cin.tie(NULL);
cin.sync_with_stdio(false);
cin >> cnt;
int* arr = new int[cnt];
for (int i = 0; i < cnt; i++) {
cin >> a >> b;
arr[i] = a + b;
}
for (int i = 0; i < cnt; i++) {
cout << arr[i] << "\n";
}
return 0;
}
문제 5. N 찍기
#include <iostream>
using namespace std;
int main() {
int cnt = 0;
cin.tie(NULL);
cin.sync_with_stdio(false);
cin >> cnt;
for (int i = 1; i <= cnt; i++) {
cout << i << "\n";
}
return 0;
}
문제 6. 기찍 N
#include <iostream>
using namespace std;
int main() {
int cnt = 0;
cin.tie(NULL);
cin.sync_with_stdio(false);
cin >> cnt;
for (int i = cnt; i > 0; i--) {
cout << i << "\n";
}
return 0;
}
문제 7. A+B - 7
#include <iostream>
using namespace std;
int main() {
int cnt = 0, a, b;
cin >> cnt;
int* arr = new int[cnt];
for (int i = 0; i < cnt; i++) {
cin >> a >> b;
arr[i] = a + b;
}
for (int i = 0; i < cnt; i++) {
cout << "Case #" << i+1 << ": "<< arr[i] << endl;
}
return 0;
}
문제 8. A+B - 8
#include <iostream>
using namespace std;
int main() {
int cnt = 0;
cin >> cnt;
int* arr = new int[cnt];
int* a = new int[cnt];
int* b = new int[cnt];
for (int i = 0; i < cnt; i++) {
cin >> a[i] >> b[i];
arr[i] = a[i] + b[i];
}
for (int i = 0; i < cnt; i++) {
cout << "Case #" << i+1 << ": " << a[i] << " + " << b[i] << " = " << arr[i] << endl;
}
return 0;
}
문제 9. 별 찍기 - 1
#include <iostream>
using namespace std;
int main() {
int cnt = 0;
cin >> cnt;
for (int i = 0; i < cnt; i++) {
for (int j = 0; j <= i; j++) {
cout << "*";
}
cout << "\n";
}
return 0;
}
문제 10. 별 찍기 - 2
#include <iostream>
using namespace std;
int main() {
int cnt = 0;
cin >> cnt;
for (int i = 1; i <= cnt; i++) {
for (int j = cnt; j >= 1; j--) {
if (i < j) {
cout << " ";
}
else {
cout << "*";
}
}
cout << "\n";
}
return 0;
}
문제 11. X보다 작은 수
#include <iostream>
using namespace std;
int main() {
int n, x;
cin >> n >> x;
int* arr = new int[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
for (int i = 0; i < n; i++) {
if (arr[i] < x) {
cout << arr[i];
if (i != n - 1) {
cout << " ";
}
}
}
return 0;
}
#include <iostream>
using namespace std;
int main() {
int a, b;
cin >> a;
cin >> b;
if (a > b) {
cout << ">";
}
else if (a < b) {
cout << "<";
}
else {
cout << "==";
}
return 0;
}
문제 2. 시험 성적
#include <iostream>
using namespace std;
int main() {
int a;
string b = "F";
cin >> a;
if (a >= 60) {
b = "D";
}
if (a >= 70) {
b = "C";
}
if (a >= 80) {
b = "B";
}
if (a >= 90) {
b = "A";
}
cout << b;
return 0;
}
문제 3. 윤년
#include <iostream>
using namespace std;
int main() {
int a, answer = 0;
cin >> a;
if(a % 4 == 0 && a % 100 != 0) {
answer = 1;
}
if (a % 400 == 0) {
answer = 1;
}
cout << answer;
return 0;
}
문제 4. 사분면 고르기
#include <iostream>
using namespace std;
int main() {
int x, y, answer = 0;
cin >> x;
cin >> y;
if (x > 0 && y > 0) {
answer = 1;
}
if (x < 0 && y > 0) {
answer = 2;
}
if (x < 0 && y < 0) {
answer = 3;
}
if (x > 0 && y < 0) {
answer = 4;
}
cout << answer;
return 0;
}
문제 5. 알람 시계
#include <iostream>
#include <stdio.h>
using namespace std;
int main() {
int h, m;
char answer[30] = "";
cin >> h;
cin >> m;
m -= 45;
if (m >= 60) {
m -= 60;
h += 1;
}
if (m < 0) {
m = 60 + m;
h -= 1;
}
if (h >= 24) {
h -= 24;
}
if (h < 0) {
h = 24 + h;
}
snprintf(answer, 30, "%d %d", h, m);
cout << answer;
return 0;
}
추가 문제 6. 오븐 시계 (브론즈 4, 2022-02-16 확인)
#include<iostream>
using namespace std;
int main() {
int a, b, c;
cin >> a >> b >> c;
b = b + c;
if (b < 0) {
b = 0;
}
while (b >= 60) {
b -= 60;
a += 1;
}
if (a < 0) {
a = 0;
}
while (a >= 24) {
a -= 24;
}
cout << a << " " << b << "\n";
return 0;
}
추가 문제 7. 주사위 세개(브론즈 4, 2022-02-16)
#include<iostream>
#include<map>
using namespace std;
int main() {
map<int, int> m;
int a, res=0, k_max = 0, v_max = 0;
for (int i = 0; i < 3; i++) {
cin >> a;
m[a]++;
if (k_max < a) k_max = a;
if (v_max < m[a]) v_max = m[a];
}
for (auto v : m) {
if (v_max != 1 && v_max == v.second) {
k_max = v.first;
}
}
if (v_max == 3) {
res = 10000 + k_max * 1000;
}
if (v_max == 2) {
res = 1000 + k_max * 100;
}
if (v_max == 1) {
res = k_max * 100;
}
cout << res << "\n";
return 0;
}
int main(){
int a, b;
scanf("%d %d", &a, &b);
printf("%d", a+b);
return 0;
}
문제 6. A-B
int main(){
int a, b;
scanf("%d %d", &a, &b);
printf("%d", a-b);
return 0;
}
문제 7. A*B
#include <iostream>
int main(){
int A, B;
std::cin >> A >> B;
std::cout << A * B;
return 0;
}
문제 8. A/B
#include <iostream>
using namespace std;
int main() {
double a, b;
cin >> a >> b;
printf("%.10f", a / b);
return 0;
}
문제 9. 사칙연산
#include <iostream>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
cout << a + b << endl;
cout << a - b << endl;
cout << a * b << endl;
cout << a / b << endl;
cout << a % b << endl;
return 0;
}
문제 10. 나머지
#include <iostream>
using namespace std;
int main() {
int a, b, c;
cin >> a >> b >> c;
cout << (a + b) % c << endl;
cout << ((a%c) + (b%c))%c << endl;
cout << (a * b)%c << endl;
cout << ((a % c) * (b % c)) % c << endl;
return 0;
}
문제 11. 곱셈
#include <iostream>
using namespace std;
int main() {
int a, b, c;
int b1, b2, b3;
cin >> a;
cin >> b;
c = a * b;
b3 = a * (b / 100);
b %= 100;
b2 = a * (b / 10);
b %= 10;
b1 = a * b;
cout << b1 << endl;
cout << b2 << endl;
cout << b3 << endl;
cout << c << endl;
return 0;
}