[TIL] 211019

Jaewon·2021년 10월 19일
0

TIL

목록 보기
10/11
post-custom-banner

1. 백준 (C++)

#include <iostream>
using namespace std;


int main() {
	int a, b, c;
	cin >> a >> b >> c;
	if (b < c) {
		cout << a / (c - b) + 1;
	}
	else {
		cout << "-1";
	}
	
}
#include <iostream>
using namespace std;
int fac(int n) {
	if (n <= 1) {
		return 1;
	}
	else {
		return n * fac(n - 1);
	}
}

int main() {
	int m;
	cin >> m;
	cout << fac(m);
}
#include <iostream>
using namespace std;
int fibo(int n) {
	if (n == 0) {
		return 0;
	}
	else if (n == 1) {
		return 1;
	}
	else {
		return fibo(n - 1) + fibo(n - 2);
	}
}

int main() {
	int m;
	cin >> m;
	cout << fibo(m);
}
#include <iostream>
#include <vector>
using namespace std;
int coin[10];
int main() {
	int N, K;
	cin >> N >> K;
	int ans = 0;
	//vector<int> coin(N);
	for (int i = 0; i < N; i++) {
		cin >> coin[i];
	}
	for (int i = N - 1; K > 0;) {
		if (coin[i] > K) {
			i--;
		}
		else {
			K -= coin[i];
			ans++;
		}
	}
	cout << ans;
}
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
	int N, start, end;
	cin >> N;
	vector<pair<int, int>> meeting;
	for (int i = 0; i < N; i++) {
		cin >> start >> end;
		meeting.push_back(make_pair(end, start));
	}
	sort(meeting.begin(), meeting.end());

	int time = meeting[0].first;
	//cout << time;
	int cnt = 1;
	for (int i = 1; i < N; i++) {
		if (time <= meeting[i].second) {
			cnt++;
			time = meeting[i].first;
		}
	}
	cout << cnt;
}
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
	int N, k,ans=0;
	cin >> N;
	vector<int> line;

	for (int i = 0; i < N; i++) {
		cin >> k;
		line.push_back(k);
	}
	sort(line.begin(), line.end());

	for (int i = 0; i < N; i++) {
		ans += line[i] * (N - i);
	}
	cout << ans;
}
#include <iostream>
#include <string>
using namespace std;

int main() {
	string str, tmp ="";
	int ans = 0;
	bool minus = false;
	cin >> str;
	for (int i = 0; i < str.size()+1; i++) {
		//cout << "str : " << str[i] << "\n";
		if (str[i] == '+' || str[i] == '-' || str[i] == '\0') {
			if (minus) {
				ans -= stoi(tmp);
			}
			else {
				ans += stoi(tmp);
			}
			if (str[i] == '-') {
				minus = true;
			}
			tmp = "";
			//cout << "ans : " << ans << "\n";
		}
		else {
			tmp += str[i];
			
		}
	}
	cout << ans;
}
#include <iostream>
using namespace std;

long long city[100001];
long long price[100001];

int main() {
	int N;
	long long ans =0;
	cin >> N;
	
	for (int i = 0; i < N-1; i++) {
		cin >> city[i];
		//cout << city[i];
	}
	for (int i = 0; i < N; i++) {
		cin >> price[i];
	}
	int m = price[0];
	for (int i = 0; i < N - 1; i++) {
		if (price[i] < m) {
			m = price[i];
		}
		ans += (long long)m * city[i];
	}
	cout << ans;
}

💡 int로 하면 범위 초과하기 때문에 long long으로 쓸 것

#include <iostream>
#include <queue>
using namespace std;
#define MAX 1001

int n, m, v;
int map[MAX][MAX];
bool visited[MAX];
queue<int> q;

void reset() {
	for (int i = 0; i <= n; i++) {
		visited[i] = false;
	}
}

void DFS(int v) {
	visited[v] = true;
	cout << v << " ";

	for (int i = 0; i <= n; i++) {
		if (map[v][i] == 1 && visited[i] == false) {
			DFS(i);
		}
	}
}

void BFS(int v) {
	q.push(v);
	visited[v] = true;

	while (!q.empty()) {
		v = q.front();
		q.pop();
		cout << v << " ";
		for (int i = 1; i <= n; i++) {
			if (map[v][i] == 1 && visited[i] == false) {
				q.push(i);
				visited[i] = true;
				//cout << i << " ";
			}
		}
	}
}

int main() {
	cin >> n >> m >> v;
	for (int i = 0; i < m; i++) {
		int a, b;
		cin >> a >> b;
		map[a][b] = 1;
		map[b][a] = 1;
	}

	reset();
	DFS(v);

	cout << "\n";

	reset();
	BFS(v);
}
  1. bfs
#include <iostream>
#include <queue>
#define MAX 101
using namespace std;
int n, m;
int map[MAX][MAX] = { 0, };
bool visited[MAX] = { 0, };
queue<int> q;
int cnt = 0;

void bfs(int v) {
	q.push(v);
	visited[v] = true;
	while (!q.empty()) {
		v = q.front();
		q.pop();
		//cout << v << " ";
		for (int i = 1; i <= n; i++) {
			if (map[v][i] == 1 && visited[i] == false) {
				q.push(i);
				visited[i] = true;
				cnt++;
			}
		}
	}
	cout << cnt;
}

int main() {
	//cout << visited[2];
	cin >> n >> m;
	for (int i = 0; i < m; i++) {
		int a, b;
		cin >> a >> b;
		map[a][b] = 1;
		map[b][a] = 1;
	}
	bfs(1);
}
  1. dfs
#include <iostream>
#define MAX 101
using namespace std;
int n, m;
int map[MAX][MAX] = { 0, };
bool visited[MAX] = { 0, };
int cnt = 0;

void dfs(int v) {
	visited[v] = true;
	cnt++;
	//cout << v << " ";
	for (int i = 0; i <= n; i++) {
		if (map[v][i] == 1 && visited[i] == false) {
			dfs(i);
		}
	}
}
int main() {
	//cout << visited[2];
	cin >> n >> m;
	for (int i = 0; i < m; i++) {
		int a, b;
		cin >> a >> b;
		map[a][b] = 1;
		map[b][a] = 1;
	}
	dfs(1);
	cout << cnt-1;
}
post-custom-banner

0개의 댓글