본문 바로가기

전체 글293

[C] 구조체 예제 10, 20을 입력받아 출력하는 구조체를 프로그램하세요 #include #include struct student{ int number; }; int main(){ struct student s; s.number = 10; printf("%d\n", s.number); struct student *pS; pS = &s; (*pS).number = 20; printf("%d\n", (*pS).number); return 0; } #include typedef struct point{ int x; int y; }POINT; POINT translate(POINT a, POINT b){ POINT newP; newP.x = a.x + b.x; newP.y = a.y + b.y; return newP; } in.. 2021. 3. 12.
[C] getch / putch getch(); 정의 : 문자 하나를 입력받는 함수. 대기함수로서 문자가 입력될 때까지 기다린다. #include #include int main(){ int word; printf("프로그램을 계속 진행하시겠습니까? (y/n)\n"); word = getch(); if(word == 'y'){ printf("\n%c 를 입력하셨습니다.\n", word); } else{ printf("\n프로그램을 종료합니다.\n"); } return 0; } getch 를 사용하려면 함수를 사용한다. - getche(); 라는 함수도 있다. 이는 메아리(Echo) 처럼 입력받은걸 화면에 확인시키듯 보여준다. #include #include int main(){ int word; word = getche(); print.. 2021. 3. 12.
[C] 그래프 알고리즘 (Graph Algorithm) 정의 : - 정점과 간선으로 구성하는 자료 구조ㅠㄷ 용어 : - 정점, 노드(Vertex, Node) : 여러 특성을 가질 수 있는 객체 - 간선(Edge, link) : 각 노드를 연결하는 선. 화살표나 직선 등 형태는 다양하다. - self-loops : 노드에서 자기 자신으로 돌아오는 간선. - Adjacency : 방향 그래프(Directed Graph) 에서 간선으로 연결된 이동가능한 인접 노드. 방향그래프의 간선은 편도이므로 역방향의 노드는 Adjacency 가 아니다. - Degree : 1) 각 노드에 연결된 간선의 수. 2) in-degree 는 해당 노드로 들어오는 간선. out-degree 는 해당 노드를 나가는 간선. 3) Degree = in-degree + out-degree -.. 2021. 3. 11.
[C] 삽입 / 선택 / 버블 정렬 배열 자료값의 갯수 만큼 표적 자료와 다른 자료를 비교하며 회차를 반복하여 정렬시킨다. 삽입 정렬 : 자료들을 비교해가며 최댓값을 찾는데, 최댓값이 찾아지면 그 값은 이전 값들과 모두 비교하여 최댓값의 자리로 간다. - 1회차를 돌리면 배열 중 가장 큰 최댓값을 구할 수 있으므로, 최댓값을 구할 때 1회차만 돌림으로써 빨리 찾는데 유용. 선택 정렬 : 자료들을 비교해가며 최솟값을 찾는데, 최솟값이 찾아지면 그 값은 이후 값들과 모두 비교하여 최솟값의 자리에 머문다. 버블 정렬 : 인접한 두 값을 연속적으로 비교해가며 최댓값을 찾는다. 최댓값으로 선택된 값은 계속 우측으로 이동하며 최댓값 자리로 이동한다. - 삽입 정렬과 마찬가지로 1회차 만에 최댓값을 찾는데 용이하다 2021. 3. 10.
[C] 구조체 예제 #include #include struct student{ int number; char name[10]; }; int main(){ struct student s1; s1.number = 1; strcpy(s1.name, "JJ"); printf("%d", s1.number); printf("%s", s1.name); return 0; } #include #include #include struct date{ int month; int day; }; struct student{ int number; struct date d; char name[10]; }; int main(){ struct student s; s.number = 1; s.d.month = 3; s.d.day = 10; strcpy(s.. 2021. 3. 10.
[Python] 집합 정의 : - 집합(set) 이란 요소를 그룹화하여 다루는 형 특징 : - 리스트(list) 와 비슷하지만 요소에 순서는 없다. - 동일한 요소를 중복하여 보유 불가. 즉 값이 중복되지 않는다. - { } 를 요소로 둘러싸서 정의 - 딕셔너리 생성 방식과 비슷하다. ex) a = { 'A', 'B', 'C', 'D' } 기능 : - 다른 데이터를 이용해 '집합' 생성 1) 문자열, 리스트, 튜플 등에서 집합으로 변환 가능하며, set(문자열 / 리스트 / 튜플) 형식으로 선언하면 된다. list = { 1, 2, 3, 4 } example_a = set(list) - 집합의 크기를 알려면 len() 함수를 사용 가능하며, 리스트(list) 나 딕셔너리(dict) 의 크기를 확인할 때도 이용 가능 list .. 2021. 3. 10.
[C] 문자열 예제 #include int main(){ int i; char str[4] = { 'a', 'b', 'c' }; i = 0; while(str[i] != '\0'){ printf(" %c", str[i] ); i++; } return 0; } '\0' 은 NULL 값을 의미한다. 배열에 자료값이 없어 NULL이면 while 문을 나온다. #include #include int main(void){ int i; char str[] = "hello"; // str = 'JJ'; strcpy(str, "JJ"); printf("%s", str); char *p = "hi~~~"; return 0; } #include #include int main(void){ int i; char str[] = "hello"; .. 2021. 3. 9.
[C] 포인터 예제 a[] = {10, 20, 30, 40, 50} 을 거꾸로 출력하는 프로그램을 작성하세요. (포인터 & while 활용) #include void print_reverse(int a[], int size){ int i = 4; int *p = NULL; p = a; while (i > -1){ printf("%d ", *(p+i) ); i--; } } int main(){ int a[] = {10, 20, 30, 40, 50}; print_reverse(a,5); return 0; } swap 을 활용하여 a = 100, b = 200 두 수 값을 바꾸세요. #include void swap(int *x, int *y){ int tmp; tmp = *x; *x = *y; *y = tmp; } int ma.. 2021. 3. 9.