본문 바로가기
프로그래머스코딩테스트연습풀이/C언어

[프로그래머스/C언어] 할 일 목록

by 코코쵸마 2023. 8. 13.

문제

오늘 해야 할 일이 담긴 문자열 배열 todo_list와 각각의 일을 지금 마쳤는지를 나타내는 boolean 배열 finished가 매개변수로 주어질 때, todo_list에서 아직 마치지 못한 일들을 순서대로 담은 문자열 배열을 return 하는 solution 함수를 작성해 주세요.

 

솔루션

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

// todo_list_len은 배열 todo_list의 길이입니다.
// finished_len은 배열 finished의 길이입니다.
// 파라미터로 주어지는 문자열은 const로 주어집니다. 변경하려면 문자열을 복사해서 사용하세요.
char** solution(const char* todo_list[], size_t todo_list_len, bool finished[], size_t finished_len) {
    // return 값은 malloc 등 동적 할당을 사용해주세요. 할당 길이는 상황에 맞게 변경해주세요.
    char** answer = (char**)malloc(todo_list_len * finished_len * sizeof(char*));
    for(int i = 0; i < todo_list_len; i++)
        answer[i] = (char*)malloc(20 * sizeof(char));
    int n = 0;
    int j;
    for(int i = 0; i < finished_len; i++){
        if(!finished[i]){
            for(j = 0; j < strlen(todo_list[i]); j++)
                answer[n][j] = todo_list[i][j];
            answer[n][j] = '\0';
            n++;
        }
    }
    return answer;
}

https://school.programmers.co.kr/learn/courses/30/lessons/181885

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr