#include <iostream>
using namespace std;
int main() {
cin.tie(NULL);
ios_base::sync_with_stdio(false);
while (true) {
int x, y, z;
cin >> x >> y >> z;
if (x == 0 && y == 0 && z == 0) break;
if (x >= y && x >= z) {
if (y * y + z * z == x * x) cout << "right" << '\n';
else cout << "wrong" << '\n';
}
else if (y >= x && y >= z) {
if (x * x + z * z == y * y) cout << "right" << '\n';
else cout << "wrong" << '\n';
}
else if (z >= x && z >= y) {
if(x * x + y * y == z * z) cout << "right" << '\n';
else cout << "wrong" << '\n';
}
}
return 0;
}
class Graph:
def __init__(self, n):
self.data = {}
for i in range(1, n + 1):
self.data[i] = set()
def add_edge(self, n, m):
self.data[n].add(m)
self.data[m].add(n)
def dfs(self, start):
visited = [False] * (len(self.data) + 1)
stack = []
stack.append(start)
while stack:
cur = stack.pop()
if not visited[cur]:
print(cur, end=" ")
visited[cur] = True
temp = []
for i in self.data[cur]:
if not visited[i]:
temp.append(i)
temp.sort()
temp.reverse()
stack.extend(temp)
def bfs(self, start):
import collections
visited = [False] * (len(self.data) + 1)
queue = collections.deque()
queue.append(start)
while queue:
cur = queue.popleft()
if not visited[cur]:
print(cur, end=' ')
visited[cur] = True
temp = []
for i in self.data[cur]:
if not visited[i]:
temp.append(i)
temp.sort()
queue.extend(temp)
from sys import stdin
node_edge_start = list(map(int, stdin.readline().split()))
graph = Graph(node_edge_start[0])
li = []
for i in range(0, node_edge_start[1]):
rawInput = list(map(int, stdin.readline().split()))
graph.add_edge(rawInput[0], rawInput[1])
graph.dfs(node_edge_start[2])
print()
graph.bfs(node_edge_start[2])
C++로 짠 코드. 자식이 여러개라면 작은 수부터 방문한다는 조건이 있어서 <algorithm>의 sort() 함수를 이용해서 정렬해주었다.
#include <iostream>
#include <queue>
#include <tuple>
#include <vector>
#include <algorithm>
#include <stack>
using namespace std;
bool cmp(int a, int b) {
return a > b;
}
vector<int> w[1001];
int n, m, startNode;
void dfs() {
vector<bool> visited(n + 1);
stack<int> stack;
stack.push(startNode);
while (!stack.empty()) {
int cur = stack.top();
stack.pop();
if (!visited[cur]) {
cout << cur << " ";
}
visited[cur] = true;
for (int v : w[cur]) {
if (!visited[v])
stack.push(v);
}
}
}
void bfs() {
vector<bool> visited(n + 1);
queue<int> q;
q.push(startNode);
while (!q.empty()) {
int cur = q.front();
q.pop();
if (!visited[cur])
cout << cur << " ";
visited[cur] = true;
for (int v : w[cur]) {
if (!visited[v]) {
q.push(v);
}
}
}
}
int main() {
cin.tie(NULL);
ios_base::sync_with_stdio(false);
cin >> n >> m >> startNode;
int v1, v2;
for (int i = 0; i < m; i++) {
cin >> v1 >> v2;
w[v1].push_back(v2);
w[v2].push_back(v1);
}
for (int i = 1; i <= n; i++) {
sort(w[i].begin(), w[i].end(), cmp);
}
dfs();
cout << endl;
for (int i = 1; i <= n; i++) {
sort(w[i].begin(), w[i].end());
}
bfs();
return 0;
}
1은 소수가 아니라는 거 아주 잘 알면서도 코드 짤 때에는 그걸 생각을 안 하고 코드를 짠다.
소수 문제 풀 때 꼭 기억하자 1은 소수가 아니다!
1은 따로 예외로 두고 코드를 짜자.
#include <iostream>
#include <vector>
using namespace std;
int main() {
int m, n;
cin >> m >> n;
vector<int> v;
for (int i = m; i <= n; i++) {
bool flag = false;
for (int j = 2; j < i / 2 + 1; j++) {
if (i % j == 0) {
flag = true;
break;
}
}
if (i == 1) flag = true;
if (!flag) v.push_back(i);
}
if (v.size() == 0) cout << -1 << endl;
else {
int total = 0;
for (int i = 0; i < v.size(); i++) {
total += v[i];
}
cout << total << endl;
cout << v[0] << endl;
}
return 0;
}
#include <iostream>
using namespace std;
int main() {
int a, b, v;
cin >> a >> b >> v;
v -= a;
int day = 1;
day += (v / (a - b));
if (v % (b - a) != 0)
day++;
cout << day;
return 0;
}