Skip to content

Commit

Permalink
add file extension
Browse files Browse the repository at this point in the history
  • Loading branch information
zimiin committed Mar 2, 2021
1 parent 7225bdc commit 737d64e
Show file tree
Hide file tree
Showing 94 changed files with 5,517 additions and 0 deletions.
46 changes: 46 additions & 0 deletions Hackerearth 1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <iostream>
#include <queue>
using namespace std;

#define MAX 100000

int main() {
priority_queue<int> pq;
int N; // 배열 크기
int A[MAX]; // 입력 배열
int tmp[3]; // 곱 계산 시 사용
long long mul; // 각 인덱스에서의 곱 저장


// 입력
cin >> N;
for (int i = 0; i < N; i++) cin >> A[i];

// -1 프린트
cout << "-1\n" << "-1\n";

// 큐에 삽입
pq.push(A[0]);
pq.push(A[1]);

// 2번 인덱스부터 최대 3개 숫자의 곱 출력
for (int i = 2; i < N; i++) {
pq.push(A[i]);

// 최댓값 3개의 곱 계산
mul = 1;
for (int i = 0; i < 3; i++) {
tmp[i] = pq.top();
pq.pop();

mul *= tmp[i];
}

// 큐에 다시 넣어줌
for (int i = 0; i < 3; i++) pq.push(tmp[i]);

cout << mul << '\n';
}

return 0;
}
27 changes: 27 additions & 0 deletions 백준 1003.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// 1003번 피보나치 함수

#include <cstdio>
#include <utility>
using namespace std;

pair<int, int> noZeroOne[41];
int t;

int main() {
// calculate no. of zeros and ones
noZeroOne[0] = make_pair(1, 0);
noZeroOne[1] = make_pair(0, 1);

for (int i = 2; i <= 40; i++) {
noZeroOne[i].first = noZeroOne[i - 1].first + noZeroOne[i - 2].first;
noZeroOne[i].second = noZeroOne[i - 1].second + noZeroOne[i - 2].second;
}

// print the result
scanf("%d", &t);
while (t--) {
int n;
scanf("%d", &n);
printf("%d %d\n", noZeroOne[n].first, noZeroOne[n].second);
}
}
27 changes: 27 additions & 0 deletions 백준 1010.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// 1010번 다리 놓기

#include <cstdio>
using namespace std;

int bin[31][31];
int n, m, t;

int main() {
bin[0][0] = 1;
for (int i = 1; i < 31; i++) {
bin[i][0] = bin[i - 1][0];

for (int j = 0; j <= i; j++) {
bin[i][j] = bin[i - 1][j] + bin[i - 1][j - 1];
}
}

scanf("%d", &t);
while (t--) {
scanf("%d%d", &n, &m);
printf("%d\n", bin[m][n]);

}

return 0;
}
102 changes: 102 additions & 0 deletions 백준 1012.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// 백준 1012번 유기농 배추

#include <iostream>
#include <queue>
using namespace std;

bool field[50][50];
int N, M; // 배추밭 세로, 가로

// 배추밭 정보 입력
void input_field() {
int K; // 배추 개수
int x, y;

cin >> M >> N >> K;

for (int i = 0; i < K; i++) {
cin >> x >> y;
field[y][x] = true;
}
}

// 배추밭 출력
void print_field() {
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
cout << field[i][j] << " ";
}
cout << "\n";
}
}

// 인덱스 y, x가 유효한지
bool valid(int y, int x) {
// index out of range
if (y < 0 || y >= N || x < 0 || x >= M) return false;

// field[y][x]가 배추가 아님
if (!field[y][x]) return false;

return true;
}

// field[y][x]부터 BFS수행
void BFS(int y, int x) {
queue<pair<int, int>> q;
int ypos[4] = {-1, 0, 1, 0};
int xpos[4] = {0, 1, 0, -1};

q.push(make_pair(y, x));
field[y][x] = 0;

while (!q.empty()) {
y = q.front().first;
x = q.front().second;
q.pop();

for (int i = 0; i < 4; i++) {
int ny = y + ypos[i];
int nx = x + xpos[i];

if (valid(ny, nx)) {
q.push(make_pair(ny, nx));
field[ny][nx] = 0;
}
}
}
}

// 필요한 지렁이 수 계산
int count_worm() {
int worm = 0;

for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
// 이미 방문했거나 배추가 아님
if (field[i][j] != 1) continue;

// 현재 위치부터 BFS수행
BFS(i, j);

// 지렁이 수 증가
worm++;
}
}

return worm;
}

int main() {
int T;
cin >> T;

while (T--) {
// 배추밭 정보 입력
input_field();
// 필요한 지렁이 수 출력
cout << count_worm() << "\n";
}

return 0;
}
40 changes: 40 additions & 0 deletions 백준 10610.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// 10610 30
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

bool cmp(int a, int b) {
return a > b ? true : false;
}

int main() {
int N[100000];
int len;

string input;
cin >> input;

for (len = 0; input[len]; len++) {
N[len] = input[len] - '0';
}

int cnt0 = 0, sum = 0;
for (int i = 0; i < len; i++) {
sum += N[i];
if (N[i] == 0) cnt0++;
}

if (cnt0 == 0 || sum % 3 != 0) {
cout << -1;
return 0;
}

sort(N, N + len, cmp);

for (int i = 0; i < len; i++) {
cout << N[i];
}

return 0;
}
94 changes: 94 additions & 0 deletions 백준 1062.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// 1062번 가르침

#include <iostream>
using namespace std;

int N, K;

bool alph[26] = { 0 };
char words[50][16];
int selectCnt = 0;
int maxCnt = 0;

void input() {
cin >> N >> K;

for (int i = 0; i < N; i++) {
cin >> words[i];
}
}

void updateMax() {
int res = 0;

for (int i = 0; i < N; i++) {
int j = 0;
for (; words[i][j]; j++) {
if (alph[words[i][j] - 'a'] == false) break;
}

if (words[i][j] == '\0') res++;
}

if (maxCnt < res) maxCnt = res;
}

void init() {
// select a, n, t, i, c
alph['a' - 'a'] = true;
alph['n' - 'a'] = true;
alph['t' - 'a'] = true;
alph['i' - 'a'] = true;
alph['c' - 'a'] = true;

selectCnt = 5;
updateMax();
}

void dfs(int index) {
// 1. 체크인
alph[index] = true;
selectCnt++;

// 2. 목적지인가?
if (selectCnt == K) {
updateMax();

alph[index] = false;
selectCnt--;
return;
}

// 3. 연결 순회
for (int i = index + 1; i < 26; i++) {
// 4. 갈 수 있는가?
if (!alph[i]) {
// 5. 간다
dfs(i);
}
}

// 6. 체크아웃
alph[index] = false;
selectCnt--;
}

int solution() {
if (K < 5) return 0;

init();

for (int i = 1; i < 26; i++) {
if (!alph[i]) {
dfs(i);
}
}

return maxCnt;
}

int main() {
input();
cout << solution();
return 0;
}
38 changes: 38 additions & 0 deletions 백준 1072.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// 1072번 게임

#include <iostream>
using namespace std;

int main() {
long long X, Y;
long long rate;

cin >> X >> Y;
rate = Y * 100 / X;

if (rate >= 99) {
cout << -1;
return 0;
}

long long low = 1, high = 1000000000, mid;
long long ans = 0;
long long curRate;

while (low <= high) {
mid = (low + high) / 2;

curRate = (Y + mid) * 100 / (X + mid);
if (curRate > rate) {
ans = mid;
high = mid - 1;
}
else {
low = mid + 1;
}
}

cout << ans;

return 0;
}
Loading

0 comments on commit 737d64e

Please sign in to comment.