Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

4주차 과제 _ 김민혜 #10

Open
wants to merge 2 commits into
base: 4주차
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
278 changes: 278 additions & 0 deletions minhae/4주차 과제_김민혜.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,278 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "e21ff13f",
"metadata": {},
"source": [
"p.137 문제 23번. 연결 요소의 개수 구하기"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "60340567",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"6 5\n",
"1 2\n",
"2 5\n",
"5 1\n",
"3 4\n",
"4 6\n"
]
},
{
"ename": "IndexError",
"evalue": "list index out of range",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)",
"Input \u001b[0;32mIn [4]\u001b[0m, in \u001b[0;36m<cell line: 18>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 16\u001b[0m count \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m0\u001b[39m\n\u001b[1;32m 18\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m i \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mrange\u001b[39m(\u001b[38;5;241m1\u001b[39m \u001b[38;5;241m+\u001b[39m N\u001b[38;5;241m+\u001b[39m\u001b[38;5;241m1\u001b[39m):\n\u001b[0;32m---> 19\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[43mvisit\u001b[49m\u001b[43m[\u001b[49m\u001b[43mi\u001b[49m\u001b[43m]\u001b[49m:\n\u001b[1;32m 20\u001b[0m count \u001b[38;5;241m+\u001b[39m\u001b[38;5;241m=\u001b[39m \u001b[38;5;241m1\u001b[39m\n\u001b[1;32m 21\u001b[0m DFS(i)\n",
"\u001b[0;31mIndexError\u001b[0m: list index out of range"
]
}
],
"source": [
"N, M = map(int, input().split())\n",
"A = [[] for i in range(N + 1)]\n",
"visit = [False] * (N+1)\n",
"\n",
"def DFS(x):\n",
" visit[x] = True\n",
" for i in A[x]:\n",
" if not visit[i]:\n",
" DFS(i)\n",
" \n",
"for i in range(M):\n",
" f,w = map(int, input().split())\n",
" A[f].append(w)\n",
" A[w].append(f)\n",
" \n",
"count = 0\n",
"\n",
"for i in range(1 + N+1):\n",
" if not visit[i]:\n",
" count += 1\n",
" DFS(i)\n",
" \n",
"print(count)"
]
},
{
"cell_type": "markdown",
"id": "c732f09f",
"metadata": {},
"source": [
"p.152 문제 26번. DFS와 BFS 프로그램"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "0210d6fb",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5 5 3\n",
"5 4\n",
"5 2\n",
"1 2\n",
"3 4\n",
"3 1\n",
"331425"
]
}
],
"source": [
"from collections import deque\n",
"n, m, s = map(int, input().split())\n",
"A = [[] for i in range(n+1)]\n",
"\n",
"for k in range(M):\n",
" f,w = map(int, input().split())\n",
" A[f].append(w)\n",
" A[w].append(f)\n",
" \n",
"for u in range(n+1):\n",
" A[u].sort()\n",
" \n",
"def DFS(x):\n",
" print(x, end='')\n",
" visit[x] = True\n",
" for i in A[x]:\n",
" if not visit[x]:\n",
" DFS(i)\n",
" \n",
"visit = [False] * (n+1)\n",
"DFS(s)\n",
"\n",
"def BFS(x):\n",
" queue = deque()\n",
" queue.append(x)\n",
" visit[x] = True\n",
" while queue:\n",
" now_Node = queue.popleft()\n",
" print(now_Node, end = '')\n",
" for i in A[now_Node]:\n",
" if not visit[i]:\n",
" visit[i] = True\n",
" queue.append(i)\n",
" \n",
"visit = [False] * (n+1)\n",
"BFS(s)"
]
},
{
"cell_type": "markdown",
"id": "fd0f209c",
"metadata": {},
"source": [
"p.169 문제29번. 원하는 정수 찾기"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "2116d5cc",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5\n",
"4 1 5 2 3\n",
"5 \n",
"1 3 7 9 5\n",
"1\n",
"1\n",
"0\n",
"0\n",
"1\n"
]
}
],
"source": [
"N = int(input())\n",
"A = list(map(int, input().split()))\n",
"A.sort() # 이진탐색은 오름차순으로 정렬된 리스트를 활용해야 함으로\n",
"M = int(input())\n",
"target = list(map(int, input().split()))\n",
"\n",
"for i in range(M):\n",
" flag = False\n",
" t = target[i]\n",
" \n",
" start = 0\n",
" end = len(A)-1\n",
" while start <= end:\n",
" mid = int((start + end) / 2)\n",
" mid2 = A[mid]\n",
" if mid2 > t:\n",
" end = mid - 1\n",
" elif mid2 < t:\n",
" start = mid + 1\n",
" else:\n",
" flag = True\n",
" break\n",
" \n",
" if flag:\n",
" print(1)\n",
" else:\n",
" print(0)"
]
},
{
"cell_type": "markdown",
"id": "35f128e5",
"metadata": {},
"source": [
"p.183 문제32번. 동전 개수의 최솟값 구하기"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "849a6f70",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"10 4200\n",
"1\n",
"5\n",
"10\n",
"50\n",
"100\n",
"500\n",
"1000\n",
"5000\n",
"10000\n",
"50000\n",
"6\n"
]
}
],
"source": [
"n,k = map(int, input().split())\n",
"count = 0\n",
"money = []\n",
"\n",
"for i in range(n):\n",
" a = int(input())\n",
" money.append(a)\n",
" \n",
"\n",
"\n",
"money = list(reversed(money))\n",
"\n",
"for m in money:\n",
" b = k//m\n",
" count += b\n",
" k -= m * b\n",
" \n",
"print(count)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "42252371",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
3 changes: 3 additions & 0 deletions minhae/4주차 과제_김민혜
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
DFS와 BFS는 확실히 어려웠습니다! 그래서 책 코드를 많이 참고하게 된 것 같아요,,
반면 binary search와 그리디는 상대적으로 쉽게 느껴졌어요.
DFS, BFS 문제 많이 풀어야 할 것 같습니다 🥲