Hackerrank | Left Rotation

133210·2021년 7월 26일
0

2020 문제풀이

목록 보기
9/14
post-thumbnail

https://www.hackerrank.com/challenges/array-left-rotation/problem

Problem

받아온 n개의 숫자를 d 만큼 왼쪽으로 밀어서 출력하기

Code

Code1

#include <assert.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char* readline();
char** split_string(char*);



int main()
{
	char** nd = split_string(readline());

	char* n_endptr;
	char* n_str = nd[0];
	int n = strtol(n_str, &n_endptr, 10);

	if (n_endptr == n_str || *n_endptr != '\0') { exit(EXIT_FAILURE); }

	char* d_endptr;
	char* d_str = nd[1];
	int d = strtol(d_str, &d_endptr, 10);

	if (d_endptr == d_str || *d_endptr != '\0') { exit(EXIT_FAILURE); }

	char** a_temp = split_string(readline());

	int* a = malloc(n * sizeof(int));

	for (int i = 0; i < n; i++) {
		char* a_item_endptr;
		char* a_item_str = *(a_temp + i);
		int a_item = strtol(a_item_str, &a_item_endptr, 10);

		if (a_item_endptr == a_item_str || *a_item_endptr != '\0') { exit(EXIT_FAILURE); }

		*(a + i) = a_item;
	}

	for (int i = 0; i < d; i++) 	//d번 반복
	{
		int temp = 0;		//a[0]을 받아 둘 빈 칸
		temp = a[0];
		for (int j = 0; j < n - 1; j++)		//한 칸 왼쪽으로 옮기기
			a[j] = a[j + 1];
		a[n - 1] = temp;		//a[0]을 배열 끝으로 (a[n-1])
	}

	for (int i = 0; i < n; i++)	//출력
		printf("%d ", a[i]);

	return 0;
}

char* readline() {
	size_t alloc_length = 1024;
	size_t data_length = 0;
	char* data = malloc(alloc_length);

	while (true) {
		char* cursor = data + data_length;
		char* line = fgets(cursor, alloc_length - data_length, stdin);

		if (!line) { break; }

		data_length += strlen(cursor);

		if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') { break; }

		size_t new_length = alloc_length << 1;
		data = realloc(data, new_length);

		if (!data) { break; }

		alloc_length = new_length;
	}

	if (data[data_length - 1] == '\n') {
		data[data_length - 1] = '\0';
	}

	data = realloc(data, data_length);

	return data;
}

char** split_string(char* str) {
	char** splits = NULL;
	char* token = strtok(str, " ");

	int spaces = 0;

	while (token) {
		splits = realloc(splits, sizeof(char*) * ++spaces);
		if (!splits) {
			return splits;
		}

		splits[spaces - 1] = token;

		token = strtok(NULL, " ");
	}

	return splits;
}

Code2

#include <assert.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    int n, d;
    int *a = malloc(sizeof(int) * n); 
    
    scanf("%d %d", &n, &d);
    
    for(int i=0; i<n; i++)
        scanf("%d", &a[i]);

    for (int i = 0; i < d; i++)
    {
        int temp = 0;
        temp = a[0];
        for (int j = 0; j < n - 1; j++)
            a[j] = a[j + 1];
        a[n - 1] = temp;
    }
    
    for (int i = 0; i < n; i++)
        printf("%d ", a[i]);

    return 0;
}

//Code 1 실행 시 Test case 1에서 Runtime error 발생

Result

0개의 댓글