본문 바로가기

전체 글333

[프로그래머스/C언어] 문자열의 앞의 n글자 문제 문자열 my_string과 정수 n이 매개변수로 주어질 때, my_string의 앞의 n글자로 이루어진 문자열을 return 하는 solution 함수를 작성해 주세요. 솔루션 #include #include #include // 파라미터로 주어지는 문자열은 const로 주어집니다. 변경하려면 문자열을 복사해서 사용하세요. char* solution(const char* my_string, int n) { // return 값은 malloc 등 동적 할당을 사용해주세요. 할당 길이는 상황에 맞게 변경해주세요. char* answer = (char*)malloc(1000 * 1000); printf("%d ", strlen(answer)); int i; for(i = 0; i < n; i++) ans.. 2023. 8. 1.
[프로그래머스/C언어] 접미사인지 확인하기 문제 어떤 문자열에 대해서 접미사는 특정 인덱스부터 시작하는 문자열을 의미합니다. 예를 들어, "banana"의 모든 접미사는 "banana", "anana", "nana", "ana", "na", "a"입니다. 문자열 my_string과 is_suffix가 주어질 때, is_suffix가 my_string의 접미사라면 1을, 아니면 0을 return 하는 solution 함수를 작성해 주세요. 솔루션 #include #include #include #include // 파라미터로 주어지는 문자열은 const로 주어집니다. 변경하려면 문자열을 복사해서 사용하세요. int solution(const char* my_string, const char* is_suffix) { int answer = 0; in.. 2023. 8. 1.
[프로그래머스/C언어] 문자열의 뒤의 n글자 문제 문자열 my_string과 정수 n이 매개변수로 주어질 때, my_string의 뒤의 n글자로 이루어진 문자열을 return 하는 solution 함수를 작성해 주세요. 솔루션 #include #include #include // 파라미터로 주어지는 문자열은 const로 주어집니다. 변경하려면 문자열을 복사해서 사용하세요. char* solution(const char* my_string, int n) { // return 값은 malloc 등 동적 할당을 사용해주세요. 할당 길이는 상황에 맞게 변경해주세요. char* answer = (char*)malloc(1000 * sizeof(int)); int i; int j = 0; for(i = strlen(my_string) - n; i 2023. 7. 31.
[프로그래머스/C언어] 9로 나눈 나머지 문제 음이 아닌 정수를 9로 나눈 나머지는 그 정수의 각 자리 숫자의 합을 9로 나눈 나머지와 같은 것이 알려져 있습니다. 이 사실을 이용하여 음이 아닌 정수가 문자열 number로 주어질 때, 이 정수를 9로 나눈 나머지를 return 하는 solution 함수를 작성해주세요. 솔루션 #include #include #include #include // 파라미터로 주어지는 문자열은 const로 주어집니다. 변경하려면 문자열을 복사해서 사용하세요. int solution(const char* number) { int answer = 0; for(int i = 0; i < strlen(number); i++) answer += number[i]-'1'+1; answer = answer % 9; return.. 2023. 7. 31.
[프로그래머스/C언어] 글자 이어 붙여 문자열 만들기 문제 문자열 my_string과 정수 배열 index_list가 매개변수로 주어집니다. my_string의 index_list의 원소들에 해당하는 인덱스의 글자들을 순서대로 이어 붙인 문자열을 return 하는 solution 함수를 작성해 주세요. 솔루션 #include #include #include // index_list_len은 배열 index_list의 길이입니다. // 파라미터로 주어지는 문자열은 const로 주어집니다. 변경하려면 문자열을 복사해서 사용하세요. char* solution(const char* my_string, int index_list[], size_t index_list_len) { // return 값은 malloc 등 동적 할당을 사용해주세요. 할당 길이는 상황에 맞.. 2023. 7. 31.
[프로그래머스/C언어] 간단한 논리 연산 문제 boolean 변수 x1, x2, x3, x4가 매개변수로 주어질 때, 다음의 식의 true/false를 return 하는 solution 함수를 작성해 주세요. (x1 ∨ x2) ∧ (x3 ∨ x4) 솔루션 #include #include #include bool solution(bool x1, bool x2, bool x3, bool x4) { bool answer = true; bool answer1 = true; bool answer2 = true; answer1 = (!x1 && !x2)? false : true; answer2 = (!x3 && !x4)? false : true; answer = (answer1 && answer2)? true : false; return answer; }.. 2023. 7. 31.
[프로그래머스/C언어] 카운트 업 문제 정수 start와 end가 주어질 때, start부터 end까지의 숫자를 차례로 담은 리스트를 return하도록 solution 함수를 완성해주세요. 솔루션 #include #include #include int* solution(int start, int end) { // return 값은 malloc 등 동적 할당을 사용해주세요. 할당 길이는 상황에 맞게 변경해주세요. int* answer = (int*)malloc((end-start+1)*sizeof(int)); for(int i = start; i 2023. 7. 31.
[프로그래머스/C언어] 수 조작하기 1 문제 정수 n과 문자열 control이 주어집니다. control은 "w", "a", "s", "d"의 4개의 문자로 이루어져 있으며, control의 앞에서부터 순서대로 문자에 따라 n의 값을 바꿉니다. "w" : n이 1 커집니다. "s" : n이 1 작아집니다. "d" : n이 10 커집니다. "a" : n이 10 작아집니다. 위 규칙에 따라 n을 바꿨을 때 가장 마지막에 나오는 n의 값을 return 하는 solution 함수를 완성해 주세요. 솔루션 #include #include #include // 파라미터로 주어지는 문자열은 const로 주어집니다. 변경하려면 문자열을 복사해서 사용하세요. int solution(int n, const char* control) { int answer = .. 2023. 7. 31.
[프로그래머스/C언어] 마지막 두 원소 문제 정수 리스트 num_list가 주어질 때, 마지막 원소가 그전 원소보다 크면 마지막 원소에서 그전 원소를 뺀 값을 마지막 원소가 그전 원소보다 크지 않다면 마지막 원소를 두 배한 값을 추가하여 return하도록 solution 함수를 완성해주세요. 솔루션 #include #include #include // num_list_len은 배열 num_list의 길이입니다. int* solution(int num_list[], size_t num_list_len) { // return 값은 malloc 등 동적 할당을 사용해주세요. 할당 길이는 상황에 맞게 변경해주세요. int* answer = (int*)malloc((num_list_len+1) * sizeof(int)); for(int i = 0; i.. 2023. 7. 31.
[프로그래머스/C언어] 이어 붙인 수 문제 정수가 담긴 리스트 num_list가 주어집니다. num_list의 홀수만 순서대로 이어 붙인 수와 짝수만 순서대로 이어 붙인 수의 합을 return하도록 solution 함수를 완성해주세요. 솔루션 #include #include #include // num_list_len은 배열 num_list의 길이입니다. int solution(int num_list[], size_t num_list_len) { int answer = 0; int odd = 0; int even = 0; int odd_j = 1; int even_j = 1; for(int i = num_list_len - 1; i >= 0; i--){ if(num_list[i] % 2 != 0){ odd += odd_j*num_list[i.. 2023. 7. 31.