코딩해요/C

[프로그래머스/C] 250209 코딩테스트

yenas0 2025. 2. 11. 23:08
반응형

가운데 글자 가져오기

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

// 파라미터로 주어지는 문자열은 const로 주어집니다. 변경하려면 문자열을 복사해서 사용하세요.
char* solution(const char* s) {
    // return 값은 malloc 등 동적 할당을 사용해주세요. 할당 길이는 상황에 맞게 변경해주세요.
    char* answer = (char*)malloc(sizeof(char)*100);
    int len = strlen(s);
    int index=0;
    if(len%2==0){
        index = len/2 - 1;
        answer[0] = s[index];
        answer[1] = s[index+1];
        answer[2] = '\0';
    }
    else{
        index = len/2;
        answer[0] = s[index];
        answer[1] = '\0';
    }
    return answer;
}

string.h 라이브러리에서 strlen 함수를 사용해서 문자열 길이를 재고 홀짝을 나눈 다음에 했다

문자열 사용할 때는 마지막에 \0을 넣어줘야 이상한 문자열이 안나옴

 

 

 


수박수박수박수박수박수?

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

char* solution(int n) {
    // 리턴할 값은 메모리를 동적 할당해주세요.
    char* answer = (char*)malloc(n*4);
    strcpy(answer, "");
    for (int i = 0; i < n; i++) {
        if (i % 2 == 0)
            strcat(answer, "수"); // 문자열 추가
        else
            strcat(answer, "박");
    }
    return answer;
}

계속 틀렸다가..

그냥 answer에 넣으면 안되는 문제

strcat으로 문자열로 해야하고 문자열 할당도 sizeof(char)*10000이런식으로 했다가 안된 케이스가 있었다.

 

 


약수의 개수와 덧셈

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

int solution(int left, int right) {
    int answer = 0;
    for(int i=left; i<=right; i++){
        int count = 0;
        for(int j=1; j<=i; j++){
            if(i%j==0){
                count++;
            }
        }
        if(count%2==0){
            answer += i;
        }
        else{
            answer -= i;
        }
    }
    
    return answer;
}

count로 약수 개수를세서 홀짝을 판단한 다음에 했다..

처음에 count변수 for문 밖으로 뺐는데 그럼 계속 초기화 돼서 안되고 첫번째 for문 안에 넣어야됨 

 

 


문자열 내림차순으로 배치하기

 

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

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

아스키 코드값이 대문자 ABCD...abcd 이 순서가 오름차순이니 내림차순으로 배열하려면 아스키 코드값이 작은 순서로 배열하면 됨

tmp로 빈 문자열 하나 만들어 놓고 반복문 두개로 정렬시켰다..

 

 


부족한 금액 계산하기

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

long long solution(int price, int money, int count) {
    long long answer = -1;
    long long total_price = 0;
    for(int i=1; i<=count; i++){
        total_price += i*price;
    }
    answer = total_price - money;
    if(answer < 0)
        answer = 0;
    return answer;
}

전체 금액을 전부 계산한 값을 total_price로 해서 이 값에서 money를 빼서 answer러 구했다. 문제에서 금액이 부족하지 않다면 0을 return하라해서 if문을 추가해둠

 

처음에 total_price 자료형을 그냥 int로 했는데 범위에서 벗어나는 케이스가 있었는지 82점이 나왔다. long long으로 해야되는듯

반응형