본문 바로가기

프로그래머스 코딩테스트

프로그래머스 코딩테스트 입문 DAY10

첫번째 - 점의 위치 구하기

사분면은 한 평면을 x축과 y축을 기준으로 나눈 네 부분입니다. 사분면은 아래와 같이 1부터 4까지 번호를매깁니다.

  • x 좌표와 y 좌표가 모두 양수이면 제1사분면에 속합니다.
  • x 좌표가 음수, y 좌표가 양수이면 제2사분면에 속합니다.
  • x 좌표와 y 좌표가 모두 음수이면 제3사분면에 속합니다.
  • x 좌표가 양수, y 좌표가 음수이면 제4사분면에 속합니다.

x 좌표 (x, y)를 차례대로 담은 정수 배열 dot이 매개변수로 주어집니다. 좌표 dot이 사분면 중 어디에 속하는지 1, 2, 3, 4 중 하나를 return 하도록 solution 함수를 완성해주세요.

 

입출력 예

dot result
[2, 4] 1
[-7, 9] 2

 

 

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>

// dot_len은 배열 dot의 길이입니다.
int solution(int dot[], size_t dot_len) {
    int answer = 0;
    if(dot[0]>0){
        if(dot[1]>0)
            answer=1;
        else if(dot[1]<0)
            answer=4;
    }
    else if(dot[0]<0){
        if(dot[1]>0)
            answer=2;
        if(dot[1]<0)
            answer=3;
    }
    return answer;
}

x좌표가 양수일때, 음수일때

y좌표가 양수일때, 음수일때를

if문을 이용해서 나눠준 후 answer의 값을 지정해준다.

 

 

 

 

 

두번째 - 2차원으로 만들기

정수 배열 num_list와 정수 n이 매개변수로 주어집니다. num_list를 다음 설명과 같이 2차원 배열로 바꿔 return하도록 solution 함수를 완성해주세요.

num_list가 [1, 2, 3, 4, 5, 6, 7, 8] 로 길이가 8이고 n이 2이므로 num_list를 2 * 4 배열로 다음과 같이 변경합니다. 2차원으로 바꿀 때에는 num_list의 원소들을 앞에서부터 n개씩 나눠 2차원 배열로 변경합니다.

num_list n result
[1, 2, 3, 4, 5, 6, 7, 8] 2 [[1, 2], [3, 4], [5, 6], [7, 8]]

 

입출력 예

num_list n result
[1, 2, 3, 4, 5, 6, 7, 8] 2 [[1, 2], [3, 4], [5, 6], [7, 8]]
[100, 95, 2, 4, 5, 6, 18, 33, 948] 3 [[100, 95, 2], [4, 5, 6], [18, 33, 948]]

 

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>

// num_list_len은 배열 num_list의 길이입니다.
int** solution(int num_list[], size_t num_list_len, int n) {
    // return 값은 malloc 등 동적 할당을 사용해주세요. 할당 길이는 상황에 맞게 변경해주세요.
    int** answer = (int**)malloc(sizeof(int*)*num_list_len/n);
    
    int i,j;
    int k=0;
    
    for(i=0;i<num_list_len/n;i++)
        answer[i]=(int*)malloc(sizeof(int)*n);
    
    for(i=0;i<num_list_len/n;i++){
        for(j=0;j<n;j++){
            answer[i][j]=num_list[k];
            k++;
        }
    }
    return answer;
}

이 문제는 동적할당이 까다로웠다.

그래서...구글링해서 풀었다 ㅎ

i는 행, j는 열이다.

행의 수는 num_list의 길이를 n으로 나눈만큼의 값이고

열의 수는 n이다.

 

 

 

 

세번째 - 공던지기

머쓱이는 친구들과 동그랗게 서서 공 던지기 게임을 하고 있습니다. 공은 1번부터 던지며 오른쪽으로 한 명을 건너뛰고 그다음 사람에게만 던질 수 있습니다. 친구들의 번호가 들어있는 정수 배열 numbers와 정수 K가 주어질 때, k번째로 공을 던지는 사람의 번호는 무엇인지 return 하도록 solution 함수를 완성해보세요.

 

입출력 예

numbers k result
[1, 2, 3, 4] 2 3
[1, 2, 3, 4, 5, 6] 5 3
[1, 2, 3] 3 2

 

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>

// numbers_len은 배열 numbers의 길이입니다.
int solution(int numbers[], size_t numbers_len, int k) {
    int answer = 0;
    int i, count=0;
    for(i=0;i<k;i++){
        answer=numbers[count];
        if(count==numbers_len-1)
            count=1;
        else if(count==numbers_len-2)
            count=0;
        else
            count+=2;
        
    }
    return answer;
}

이 문제에서 중요한 부분은 배열의 끝부분에 도달했을때

다시 맨 앞으로 이동해야한다는 부분이다.

이를 count가 마지막 인덱스이면 1이 되도록,

마지막 하나 전 인덱스이면 0이 되도록,

나머지 경우에서는 그냥 2씩 증가하도록 구현했다.

 

 

 

 

 

네번째 - 배열 회전시키기

정수가 담긴 배열 numbers와 문자열 direction가 매개변수로 주어집니다. 배열 numbers의 원소를 direction방향으로 한 칸씩 회전시킨 배열을 return하도록 solution 함수를 완성해주세요.

 

입출력 예

numbers direction result
[1, 2, 3] "right" [3, 1, 2]
[4, 455, 6, 4, -1, 45, 6] "left" [455, 6, 4, -1, 45, 6, 4]

 

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>

// numbers_len은 배열 numbers의 길이입니다.
// 파라미터로 주어지는 문자열은 const로 주어집니다. 변경하려면 문자열을 복사해서 사용하세요.
int* solution(int numbers[], size_t numbers_len, const char* direction) {
    // return 값은 malloc 등 동적 할당을 사용해주세요. 할당 길이는 상황에 맞게 변경해주세요.
    int* answer = (int*)malloc(numbers_len*sizeof(int));
    int i;
    if(strcmp(direction,"right")==0){
        for(i=0;i<numbers_len-1;i++)
            answer[i+1]=numbers[i];
        answer[0]=numbers[i];
    }
    else{
        for(i=1;i<numbers_len;i++)
            answer[i-1]=numbers[i];
        answer[i-1]=numbers[0];
    }
        
    return answer;
}

direction이 right면

i에 있던 값을 i+1로 옮겨주고

맨 마지막 인덱스의 값을 맨 앞으로 옮겨준다.

direction이 left면

i에 있던 값을 i-1로 옮겨주고

맨 처음 인덱스의 값을 맨 뒤로 옮겨준다.