Programming

    [DS] Insert a Node at the Tail of a Linked List

    https://www.hackerrank.com/challenges/insert-a-node-at-the-tail-of-a-linked-list/problem 연결리스트의 tail, 끝 부분에 노드를 삽입하는 함수를 작성해야 한다. SinglyLinkedListNode* insertNodeAtTail(SinglyLinkedListNode* head, int data) { SinglyLinkedListNode* node = head; SinglyLinkedListNode* tmp = malloc(sizeof(SinglyLinkedList)); tmp->data = data; tmp->next = NULL; if(head==NULL){ head = tmp; return head; } while(node->n..

    Breaking the Records

    https://www.hackerrank.com/challenges/breaking-best-and-worst-records/problem 문제는 쉬운 문제인데 함수의 리턴 타입을 고려해서 풀어야하는 문제이다. int* breakingRecords(int scores_count, int* scores, int* result_count) { int *breaking=malloc(sizeof(int)*2); breaking[0]=0; breaking[1]=0; int min=scores[0], max=scores[0]; for(int i=1;iscores[i]) { breaking[1]++; min=scores[i]; } if(max

    Print the Elements of a Linked List

    https://www.hackerrank.com/challenges/print-the-elements-of-a-linked-list/problem /* * For your reference: * * SinglyLinkedListNode { * int data; * SinglyLinkedListNode* next; * }; * */ void printLinkedList(SinglyLinkedListNode* head) { SinglyLinkedListNode *node=head; while(node!=NULL){ printf("%d\n",node->data); node=node->next; } } △ 작성한 부분 연결 리스트의 원소들의 값을 출력하는 함수로, 해당 노드의 값이 NULL이 되기 전까지 연결 ..

    Kangaroo

    https://www.hackerrank.com/challenges/kangaroo/problem 두 캥거루가 같은 시간에 같은 지점에 도착하는 경우에 대해 판단하는 문제이다. 어려운 문제는 아니지만 캥거루들이 움직일 수 있는 길이의 한도가 정해져있지 않아 그 부분을 어떻게 해야하나 고민했다. char* kangaroo(int x1, int v1, int x2, int v2) { int s1=0, s2=0; if((x1v2)) return "NO"; else{ for(int i=0;;i++){ s1= x1+v1*i; s2 = x2 + v2 *i; if(s1 == s2) return "YES"; if(x1s2) return "NO"; } else{ if(s1> return "NO" 그 다음 만날 수도 있고..

    Find the Merge point of two joined linked lists

    https://www.hackerrank.com/challenges/find-the-merge-point-of-two-joined-linked-lists/problem 두 연결 리스트의 연결지점을 찾는 문제 마지막에 return tmp1->data 또는 return tmp2->data 둘다 된다. (어차피 합병 포인트는 같으니까) int findMergeNode(SinglyLinkedListNode* head1, SinglyLinkedListNode* head2) { SinglyLinkedListNode *temp1=head1; SinglyLinkedListNode *temp2=head2; while(temp1!=temp2){ if(temp1->next==NULL){ temp1=head2; } else ..

    Apple and Orange

    https://www.hackerrank.com/challenges/apple-and-orange/problem #include #include #include #include #include #include #include #include #include char* readline(); char** split_string(char*); // Complete the countApplesAndOranges function below. void countApplesAndOranges(int s, int t, int a, int b, int apples_count, int* apples, int oranges_count, int* oranges) { for(int i=0;i

    Compare two linked lists

    https://www.hackerrank.com/challenges/dynamic-array/editorial 늉늉늉 bool compare_lists(SinglyLinkedListNode* head1, SinglyLinkedListNode* head2) { while(head1->next||head2->next){ if(head1->next == NULL && head2->next != NULL) return false; else if(head1->next != NULL && head2->next == NULL) return false; if(head1->data == head2->data){ head1 = head1->next; head2 = head2->next; } else { return fal..

    Grading Students

    https://www.hackerrank.com/challenges/grading/problem 문제설명 원점수가 38점 미만일 경우 환산 점수는 원점수와 같다 원점수가 38점 이상이고 원점수보다 큰 최소의 5의 배수와 원점수의 차가 3미만일 경우 환산 점수는 그 5의 배수가 된다 차가 3 이상일 경우 원점수를 환산점수로 한다 환산점수가 40점 미만일 경우 Fail이다 int* gradingStudents(int grades_count, int* grades, int* result_count) { *result_count = grades_count; int *result_grade = malloc(sizeof(int)*grades_count); int tmp=0, div=0; for(int i=0;i=3..

    2D Array

    https://www.hackerrank.com/challenges/2d-array/problem 모래 시계에 관한 문제로, 문제가 엄청 복잡한 것처럼 설명이 되어있지만 그냥 단순하게 이차원 배열에서 원소들의 합 중 가장 큰 값을 찾는 문제이다. #include #include #include #include #include #include #include #include #include char* readline(); char** split_string(char*); // Complete the hourglassSum function below. int hourglassSum(int arr_rows, int arr_columns, int** arr) { int temp=-9999, h=0; for(in..

    Time Conversion

    https://www.hackerrank.com/challenges/time-conversion/problem hh:mm:ssAM hh:mm:ssPM 의 12시간 형식으로 사용자가 입력하면 24시간 형식으로 변환해 출력하는 프로그램이다. 문자열 관련 함수를 사용하지 않은지 오래된 것 같아서 최대한 사용하려고 했다. strtok( 자를 문자열, 기준 문자 ): 문자열을 자르는 함수 >> >> 처음에만 자를 문자열의 이름을 넣어주고, 그 다음부터는 NULL을 넣어야 한다. (참고: https://dojang.io/mod/page/view.php?id=376 ) 문자열을 자르고 없애는 것이 아니라 문자열을 자르고 그 시작 부분의 주소값으로 포인터로 반환하는 것이다. strcpy( 복사한 문자열을 붙여넣을 문자..