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

[Add] 6~14 Algo, Binary Tree #32

Open
wants to merge 4 commits into
base: main
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
10 changes: 10 additions & 0 deletions KHS/1021.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from collections import deque
a=deque()

for i in range(1,11):
a.append(i)

def Number3():
a.appendleft(a.pop())
Number3()
print(a)
7 changes: 7 additions & 0 deletions KHS/11651.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import sys
N=int(sys.stdin.readline())
a=[]
for i in range(N):
a.append(tuple(map(int,sys.stdin.readline().split())))
for k,p in sorted(a,key=lambda a:(a[1],a[0])):
print(k,p)
34 changes: 34 additions & 0 deletions KHS/13116.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import sys

def binary(input):
if input==1:
return input
elif input%2==1:
return int((input-1)/2)
elif input%2==0:
return int(input/2)

a=[list(map(int,sys.stdin.readline().split())) for i in range(int(input()))]
b=[]
c=[]
d=[]
for i,k in a:
b.append(i)
c.append(k)
while True :
if i==1 and k==1:
break
elif i==1:
k=binary(k)
c.append(k)
elif k==1:
i=binary(i)
b.append(i)
else:
k=binary(k)
c.append(k)
i=binary(i)
b.append(i)
print(10*max(set(b)& set(c)))
b.clear()
c.clear()
22 changes: 22 additions & 0 deletions KHS/1920.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import sys
N=int(input())
beforelist = list(map(int, sys.stdin.readline().split()))
M=int(input())
afterlist = list(map(int, sys.stdin.readline().split()))
beforelist.sort()

def binarySearch(after,number):
first=0
last=number-1
while(first<=last):
mid=(first+last)//2
if after==beforelist[mid]:
return 1
elif after> beforelist[mid]:
first=mid+1
elif after< beforelist[mid]:
last=mid-1
return 0

for j in afterlist:
print(binarySearch(j,N))
14 changes: 14 additions & 0 deletions KHS/2012.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import sys
N=int(sys.stdin.readline())
a=[]

for i in range(N):
a.append(int(sys.stdin.readline().rstrip()))

checker=0

for k,v in enumerate(sorted(a)):# 앞이 인덱스
if v!=k+1:
checker+=abs(v-k-1)

print(checker)
20 changes: 20 additions & 0 deletions KHS/2346.PY
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#풍선 터트리기 펑펑
from collections import deque
import sys
N=int(input())
a=deque(map(int,sys.stdin.readline().split()))

# for i in range(5):
# a.append(int(input()))
# for i in a:
# print(i,end=" ")
indexcheck=0
temp=0
while a:
print(temp)
indexcheck+=a[temp]
a.remove(a[temp])
if temp>len(a)-1:
temp-=len(a)
elif temp<-len(a):
temp+=len(a)
36 changes: 36 additions & 0 deletions KHS/2805.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import sys
input=sys.stdin.readline
N,M=map(int,input().split())
a=list(map(int,input().split()))
sum=0
min=1
max=max(a)
mid=(min+max)//2

while min<=max:
sum=0
for i in a:
if i-mid>=0:
sum+=i-mid
if sum==M:
break
elif sum>M:
min=mid+1
elif sum<M:
max=mid-1
mid=(min+max)//2
print(mid)


# def binarySearch(after,number):
# first=0
# last=number-1
# while(first<=last):
# mid=(first+last)//2
# if after==beforelist[mid]:
# return 1
# elif after> beforelist[mid]:
# first=mid+1
# elif after< beforelist[mid]:
# last=mid-1
# return 0
202 changes: 202 additions & 0 deletions KHS/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@

#include <iostream>
#include <stack>
#include <tuple>

class List;

class pair;

class Node;


class pair {
public:
pair(int classnum, int grade) {
this->classnum = classnum;
this->grade = grade;
}

pair() {}

int grade = 0;
int classnum = 0;
};

class Node {
friend List;

private:
Node() {}
// ~Node(){delete this;};

Node(int classnum, int grade) {
data = pair(classnum, grade);
}

pair data;
Node *leftlink = nullptr;
Node *rightlink = nullptr;
};

class List {
private:
Node *Head = nullptr;//루트 노드 아마 const여야하지 않을까 한번 루트는 영원한 루트
Node *Temp = nullptr;
public:
List() {
}

void insert(int a, int b) {
Node *temp = Head;
if (temp == nullptr) {
Head = new Node(a, b);
} else {
Temp = new Node(a, b);
while (1) {
if (Temp->data.classnum > temp->data.classnum) {
if (temp->rightlink == nullptr) {
temp->rightlink = Temp;
break;
} else {
temp = temp->rightlink;
}
} else if (Temp->data.classnum < temp->data.classnum) {
if (temp->leftlink == nullptr) {
temp->leftlink = Temp;
break;
} else {
temp = temp->leftlink;
}
}
}
}
}

std::tuple<Node *, Node *> Get(int key) {
Node *temp = Head;
Node *follower = Head;
while (1) {
if (temp == nullptr) {
std::cout << "탐색한 노드는 없습니다" << std::endl;
break;
}
if (temp->data.classnum > key) {
follower = temp;
temp = temp->leftlink;
} else if (temp->data.classnum < key) {
follower = temp;
temp = temp->rightlink;
} else {
std::cout << "탐색한 노드는 " << "(" << temp->data.classnum << "," << temp->data.grade << ")" << std::endl;
return std::tuple<Node *, Node *>(temp, follower);
}
}
return std::tuple<Node *, Node *>(nullptr, nullptr);
}


void Delete(int key) {
std::tuple<Node *, Node *>(result) = Get(key);//temp, follower, Get 함수 접근
Node *tempNode = std::get<0>(result);
Node *followingNode = std::get<1>(result);
Node *projectNode;
Node *parentprojectNode;
std::cout << "삭제를 목표하는 노드 " << "(" << tempNode->data.classnum << "," << tempNode->data.grade << ")"
<< std::endl;
std::cout << "그 노드의 부모 노드" << "(" << followingNode->data.classnum << "," << followingNode->data.grade << ")"
<< std::endl;

if (tempNode->rightlink == nullptr && tempNode->leftlink == nullptr) {//자식이 없는 경우의 삭제
if ((tempNode->data.classnum) > (followingNode->data.classnum)) {
followingNode->rightlink = nullptr;
delete tempNode;
} else {
followingNode->leftlink = nullptr;
}
} else if (tempNode->rightlink == nullptr && tempNode->leftlink != nullptr) {//삭제할 노드의 우는 비고, 좌는 안빔
if (followingNode->data.classnum > tempNode->rightlink->data.classnum) {
followingNode->leftlink = tempNode->rightlink;
} else {
followingNode->leftlink = tempNode->leftlink;
}
delete tempNode;
} else if (tempNode->rightlink != nullptr && tempNode->leftlink == nullptr) {//삭제할 노드의 우는 차고, 좌는 빔
if (followingNode->data.classnum > tempNode->rightlink->data.classnum) {
followingNode->rightlink = tempNode->leftlink;
} else {
followingNode->rightlink = tempNode->rightlink;
}
delete tempNode;
} else {//삭제할 노드가 자식이 2명
std::tuple<Node *, Node *>(findresult) = findrightMin(tempNode);
projectNode = std::get<0>(findresult);
parentprojectNode = std::get<1>(findresult);
std::cout << "삭제 될 노드를 대체할 우측 트리에서의 가장 작은 노드는 " << "(" << projectNode->data.classnum << ","
<< projectNode->data.grade << ")" << std::endl;
std::cout << "삭제 될 노드를 대체할 노드의 부모 노드 " << "(" << parentprojectNode->data.classnum << ","
<< parentprojectNode->data.grade << ")" << std::endl;
tempNode->data.classnum = projectNode->data.classnum;//목표하는 노드 초기화 진행
tempNode->data.grade = projectNode->data.grade;
if (parentprojectNode->leftlink == projectNode) {
parentprojectNode->leftlink = nullptr;
delete projectNode;
}
else {
parentprojectNode->rightlink = nullptr;
delete projectNode;
}
}
}

std::tuple<Node *, Node *> findrightMin(Node *currentNode) {
currentNode = currentNode->rightlink;
Node *followingNode = currentNode;
while (currentNode != nullptr) {
if (currentNode->leftlink != nullptr) { currentNode = currentNode->leftlink; }
if (currentNode->leftlink == nullptr)break;
followingNode = followingNode->leftlink;
}
return std::tuple<Node *, Node *>(currentNode, followingNode);//currentNode 목표하는 노드, followingNode 목표하는 노드의 부모
}


void inorder() {
inorder(Head);
}

void inorder(Node *currentNode) {
if (currentNode) {
inorder(currentNode->leftlink);
std::cout << "(학번: " << currentNode->data.classnum << ", 점수: "
<< currentNode->data.grade << ")" << std::endl;
inorder(currentNode->rightlink);
}
}
};

int main() {
List a;//1
a.insert(20220033, 90);
a.insert(20220050, 73);
a.insert(20220043, 99);
a.insert(20220011, 82);
a.insert(20220024, 78);
a.insert(20220046, 96);
a.insert(20220001, 72);
a.insert(20220008, 88);
a.insert(20220036, 77);
a.insert(20220056, 93);
a.insert(20220053, 82);
a.insert(20220059, 100);//1번
a.inorder(); //2번
a.Delete(20220050); //3번
a.inorder(); //4번
a.Delete(20220008); //5번
a.inorder(); //6번
a.Delete(20220056); //7번
a.inorder(); //8번
a.Get(20220056); //9번
a.Get(20220043); //10번
return 0;
}