Programming

    [DS] Get Node Value

    연결 리스트의 가장 마지막 노드를 0번 인덱스로 생각했을 때 끝 노드로부터의 인덱스가 주어지면 해당 노드의 데이터를 출력하는 프로그램이다. www.hackerrank.com/challenges/get-the-value-of-the-node-at-a-specific-position-from-the-tail/problem int getNode(SinglyLinkedListNode* head, int positionFromTail) { int size = 0; SinglyLinkedListNode* tmp = head; while(tmp->next != NULL){ size +=1; tmp = tmp->next; } tmp = head; for(int i=0;inext; } return (tmp->data);..

    Counting Valleys

    www.hackerrank.com/challenges/counting-valleys/problem 사용자로부터 U, D (uphills, downhills) 문자열을 입력받고 처음시작을 해수면(0)으로 해서 valley의 수를 구하는 코드를 작성해야 한다. int countingValleys(int steps, char* path) { int valley=0; int pre_level = 0,level =0; for(int i=0;i

    Drawing Book

    [ , 1] ▷ [2 , 3] ▷ [4, 5] ▷ .... 식으로 페이지가 체크되어 있는 책에서 원하는 페이지에 도달하기까지 넘겨야 할 최소의 페이지 수를 구하는 코드를 작성해야 한다. int pageCount(int n, int p) { int count =0; int p_count = p/2; int l_count; if(n%2 == 0){ if(p%2 == 0){ l_count = (n-p)/2; } else{ l_count = (n-p)/2 + 1; } } else{ l_count = (n-p)/2; } count = (p_count >= l_count)?l_count:p_count; return count; } △ 작성한 부분 1페이지 부터 넘기는 경우(p_count)와 가장 마지막 페이지(n)..

    [DS] Reverse a doubly-linked list

    양방향 연결 리스트 (순환X)의 방향을 바꾸는 코드를 작성해야 한다. www.hackerrank.com/challenges/reverse-a-doubly-linked-list/problem DoublyLinkedListNode* reverse(DoublyLinkedListNode* head) { DoublyLinkedListNode *link = malloc(sizeof(DoublyLinkedListNode)); DoublyLinkedListNode *rever = malloc(sizeof(DoublyLinkedListNode)); DoublyLinkedListNode *tmp = head; while(tmp ->next != NULL){ tmp = tmp->next; } rever = tmp; whil..

    [DS] Delete a Node

    www.hackerrank.com/challenges/delete-a-node-from-a-linked-list/problem position이 주어지고 해당 position의 노드를 삭제하는 코드를 작성해야 한다. SinglyLinkedListNode* deleteNode(SinglyLinkedListNode* head, int position) { SinglyLinkedListNode* temp = head; SinglyLinkedListNode* tmp; if(position == 0) { head = head->next; return head; } for(int i=0;inext; } tmp = temp->next; temp->next = tmp->next; free(tmp); return hea..

    Migratory Birds

    www.hackerrank.com/challenges/migratory-birds/problem 배열을 이용해 값을 비교해 최대의 값을 가지는 타입을 출력하는 프로그램이다. // Complete the migratoryBirds function below. int migratoryBirds(int arr_count, int* arr) { int common=0; int nums[5] = {0,}; for(int i=0; i

    [DS] Insert a node at a specific position in a linked list

    link /* * For your reference: * * SinglyLinkedListNode { * int data; * SinglyLinkedListNode* next; * }; * */ SinglyLinkedListNode* insertNodeAtPosition(SinglyLinkedListNode* head, int data, int position) { SinglyLinkedListNode *node = malloc(sizeof(SinglyLinkedListNode)); node->data = data; node->next=NULL; SinglyLinkedListNode *tmp = head; for(int i=0;inext; } node->next = tmp->next; tmp->next=..

    Divisible Sum Pairs

    www.hackerrank.com/challenges/divisible-sum-pairs/problem int divisibleSumPairs(int n, int k, int ar_count, int* ar) { int pair=0; int sum=0; for(int i=0;i

    [DS] Insert a node at the head of a linked list

    www.hackerrank.com/challenges/insert-a-node-at-the-head-of-a-linked-list/problem 지난 번 DS 문제는 말단 노드에 새로운 노드를 삽입하는 문제였다면 이번 문제는 head 노드에 새로운 노드를 삽입하는 문제이다. 이 경우가 말단에 삽입할 때보다 더 간단한 것 같다. SinglyLinkedListNode* insertNodeAtHead(SinglyLinkedListNode* llist, int data) { SinglyLinkedListNode* node = malloc(sizeof(SinglyLinkedListNode)); node->data = data; node->next = NULL; if(llist == NULL) llist = nod..

    Birthday Chocolate

    www.hackerrank.com/challenges/the-birthday-bar/problem m(month)와 d(day) 값을 이용해서 정수배열 s에서 연속된 m개의 숫자의 합이 d인 경우의 수를 출력하는 코드이다. 전체적으로 쉽고 주의해야 할 부분은 반복문의 횟수(조건 부분)이다. #include #include #include #include #include #include #include #include #include #include char* readline(); char* ltrim(char*); char* rtrim(char*); char** split_string(char*); // Complete the birthday function below. int birthday(int s..