본문 바로가기

프로그래머스 코딩테스트

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

첫번째 - 모음제거

영어에선 a, e, i, o, u 다섯 가지 알파벳을 모음으로 분류합니다. 문자열 my_string이 매개변수로 주어질 때 모음을 제거한 문자열을 return하도록 solution 함수를 완성해주세요.

 

입출력 예

my_string result
"bus" "bs"
"nice to meet you" "nc t mt y"

 

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

// 파라미터로 주어지는 문자열은 const로 주어집니다. 변경하려면 문자열을 복사해서 사용하세요.
char* solution(const char* my_string) {
    // return 값은 malloc 등 동적 할당을 사용해주세요. 할당 길이는 상황에 맞게 변경해주세요.
    char* answer = (char*)malloc(my_string);
    
    int i,j=0;
    for(i=0;my_string[i]!='\0';i++){
        if(my_string[i]=='a')
            continue;
        if(my_string[i]=='e')
            continue;
        if(my_string[i]=='i')
            continue;
        if(my_string[i]=='o')
            continue;
        if(my_string[i]=='u')
            continue;
        answer[j++]=my_string[i];
            
    }
    answer[j]='\0';
    return answer;
}

my_string을 answer에 붙여넣을때

만약 my_string[i]가 모음이라면

answer에 붙여넣지 않고 다음 반복으로 넘어간다.

 

 

 

 

 

두번째 - 문자열 정렬하기(1)

문자열 my_string이 매개변수로 주어질 때, my_string 안에 있는 숫자만 골라 오름차순 정렬한 리스트를 return 하도록 solution 함수를 작성해보세요.

 

입출력 예

my_string result
"hi12392" [1, 2, 2, 3, 9]
"p2o4i8gj2" [2, 2, 4, 8]
"abcde0" [0]

 

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

// 파라미터로 주어지는 문자열은 const로 주어집니다. 변경하려면 문자열을 복사해서 사용하세요.
int* solution(const char* my_string) {
    // return 값은 malloc 등 동적 할당을 사용해주세요. 할당 길이는 상황에 맞게 변경해주세요.
   
    int length = strlen(my_string); 
    int* answer = (int*)malloc((length + 1) * sizeof(int));
     
    int i,j=0;
    for(i=0;my_string[i]!='\0';i++)
        if(my_string[i] >= '0' && my_string[i] <= '9')
            answer[j++]=my_string[i]-'0';
    answer[j]='\0';
    int count=j;
    
    int temp, minIndex=0;
    for(i=0;i<count;i++){
        minIndex=i;
        for(j=i;j<count;j++)
            if(answer[j]<answer[minIndex])
                minIndex=j;
        temp=answer[i];
        answer[i]=answer[minIndex];
        answer[minIndex]=temp;
    }
        
    
    return answer;
}

 

my_string에 문자('0' '1'...)의 형태로 숫자가 존재하기 때문에 이러한 숫자 문자일 때

answer에 '0'만큼을 뺀 후 저장해준다.

이렇게 answer에 my_string의 숫자들을 넣어 준 후

정렬 알고리즘을 사용하여 정렬해주면 된다.

정렬 알고리즘은 제일 작은 값을 찾아 맨 앞에서부터 값을 바꿔주는 알고리즘을 사용하였다.

 

 

 

 

 

 

세번째 - 숨어있는 숫자의 덧셈

문자열 my_string이 매개변수로 주어집니다. my_string안의 모든 자연수들의 합을 return하도록 solution 함수를 완성해주세요.

 

입출력 예

my_string result
"aAb1B2cC34oOp" 10
"1a2b3c4d123" 16

 

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

// 파라미터로 주어지는 문자열은 const로 주어집니다. 변경하려면 문자열을 복사해서 사용하세요.
int solution(const char* my_string) {
    int answer = 0;
    int i;
    for(i=0;my_string[i]!='\0';i++)
        if(my_string>='0' && my_string[i]<='9')
            answer+=my_string[i]-'0';
    return answer;
}

여기 또한 숫자가 문자형태로 되어있기 때문에 

문자형태의 숫자에서 '0'을 뺀 후 덧셈을 해주면 된다.

 

 

 

 

 

네번째 - 소인수 분해

소인수분해란 어떤 수를 소수들의 곱으로 표현하는 것입니다. 예를 들어 12를 소인수 분해하면 2 * 2 * 3 으로 나타낼 수 있습니다. 따라서 12의 소인수는 2와 3입니다. 자연수 n이 매개변수로 주어질 때 n의 소인수를 오름차순으로 담은 배열을 return하도록 solution 함수를 완성해주세요.

 

입출력 예

n result
12 [2, 3]
17 [17]
420 [2, 3, 5, 7]

 

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

int* solution(int n) {
    // return 값은 malloc 등 동적 할당을 사용해주세요. 할당 길이는 상황에 맞게 변경해주세요.
    int* answer = (int*)malloc(n);
    int i, j, flag, answer_len=0;
    while(n>1){
        for(i=2;i<=n;i++){
            if(n%i==0){
                flag=0;
                for(j=0;j<answer_len;j++)
                    if(answer[j]==i)
                        flag++;
                if(flag==0){
                    answer[answer_len]=i;
                    answer_len++;
                    n /= i;
                    break;
                }
                else {
                    n /= i;
                    break;
                }
                
            }
        }
    }
    return answer;
}

n을 i로 나눴을 때 나누어 떨어진다면

i가 배열 answer에 저장되어 있는지 확인 한 후

없다면 answer에 새롭게 저장한다.

그리고 n을 i로 나누고 n이 1보다 클 때,

이 과정을 반복한다.