U E D R , A S I H C RSS

새싹교실/2020/이찌반/김재훈

1. Level1

1.1. 두개 뽑아서 더하기

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

// numbers_len은 배열 numbers의 길이입니다.
int* solution(int numbers[], size_t numbers_len) {
    // return 값은 malloc 등 동적 할당을 사용해주세요. 할당 길이는 상황에 맞게 변경해주세요.
    
    int TimesOfNumbers[201] = {0, };        //더한 결과값을 나타내는 배열, ex)1+1=2 -> 배열의 2번째 칸 값을 1 증가
    int sizeOfArr=0;        //동적할당할 배열의 크기
    
    for(int i=0; i<numbers_len; i++) {
        for(int j=i+1; j<numbers_len; j++)
            TimesOfNumbers[numbers[i]+numbers[j]]++;
    }
    for(int i=0; i<=200; i++) {
        if(TimesOfNumbers[i])
            sizeOfArr++;
    }
    
    int* answer = (int*)malloc(sizeof(int)*sizeOfArr);
    //TimesOfNumbers의 n번째 인덱스가 1이상이다 -> numbers를 더했을때 그 값이 1회 이상 나왔다. answer에 값을 추가
    int j=0;
    for(int i=0; i<=200; i++) {
        if(TimesOfNumbers[i])
            answer[j++] = i;        
    }
    return answer;
}

1.2. 2016년

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

char* solution(int a, int b) {
    int month[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    char dayOfWeek[7][4] = {"FRI", "SAT", "SUN", "MON", "TUE", "WED", "THU"};
    int numOfDays=0;
    // 리턴할 값은 메모리를 동적 할당해주세요.
    for(int i=0; i<a-1; i++)
        numOfDays+=month[i];
    numOfDays+=(b-1);
    
    char* answer = (char*)malloc(sizeof(dayOfWeek[numOfDays%7]));       //동적할당
    for(int i=0; i<sizeof(dayOfWeek[numOfDays%7]); i++)
        answer[i] = dayOfWeek[numOfDays%7][i];
    return answer;
}

1.3. 두 정수의 합

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

long long solution(int a, int b) {
    long long answer = 0;
    int min, max;
    if(a>b) {
        min = b;
        max = a;
    }
    else {
        min = a;
        max = b;
    }
    
    for(int i=min; i<=max; i++)
        answer+=i;
    return answer;
}

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

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

// 파라미터로 주어지는 문자열은 const로 주어집니다. 변경하려면 문자열을 복사해서 사용하세요.
char* solution(const char* s) {
    // return 값은 malloc 등 동적 할당을 사용해주세요. 할당 길이는 상황에 맞게 변경해주세요.
    int length = 0;
    //문자열의 길이를 알아냄
    while(1) {        
        if(s[length] == '\0')
            break;
        length++;
    }
    length++;
    
    char* answer = (char*)malloc(length);
    char temp;
    int max;
    
    //문자열 복제
    for(int i=0; i<length; i++)
        answer[i] = s[i];
    //정렬
    for(int i=0; i<length; i++) {
        max = i;
        for(int j=i+1; j<length; j++) {
            if(answer[max]<answer[j])
                max = j;
        }
        temp = answer[i];
        answer[i] = answer[max];
        answer[max] = temp;
    }
    
    return answer;
}

1.5. 문자열 다루기 기본

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

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

1.6. 문자열을 정수로 바꾸기

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

// 파라미터로 주어지는 문자열은 const로 주어집니다. 변경하려면 문자열을 복사해서 사용하세요.
int solution(const char* s) {
    bool isPlus = true;
    int i = 0;
    int answer = 0;
    
    if(s[i] == '-') {
        isPlus = false;
        i++;
    }
    else if(s[i] == '+')
        i++;
    
    while(s[i]!='\0') 
    {
        answer += (s[i] - '0');
        answer*=10;
        i++;
    }
    answer/=10;
    
    if(isPlus == false)
        answer*=-1;   
    
    return answer;
}

1.7. 약수의 합

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

int solution(int n) {
    int answer = 0;
    int i;
    
    for(i=1; i<=n; i++) {
        if(n%i==0) {
            answer += i;        //약수를 하나씩 더함  
        }
    }
    return answer;
}

1.8. 이상한 문자 만들기

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

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

1.9. 자릿수 더하기

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

int solution(int n) {
    int answer = 0;
    while(n!=0) {
        answer += n%10;
        n/=10;
    }
    return answer;
}

1.10. 자연수 뒤집어 배열로 만들기

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

int* solution(long long n) {
    int temp = n;
    int count=0;
    
    while(temp!=0) {
        temp/=10;
        count++;
    }
    // 리턴할 값은 메모리를 동적 할당해주세요.
    int* answer = (int*)malloc(sizeof(int)*count);
    int i = 0;
    while(n!=0) {
        answer[i++] = n%10;
        n/=10;
    }
    return answer;
}

1.11. 정수 제곱근 판별

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

long long solution(long long n) {
    long long answer = 0;
    
    while(1) {
        if(answer*answer<n)
            answer++;
        else if(answer*answer>n) {
            answer = -1;
            break;
        }
        else {
            answer++;
            answer = answer*answer;
            break;
        }
    }
    return answer;
}

1.12. 짝수와 홀수

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

char* solution(int num) {
    int letterNumOfReturn[2] = {5, 4};
    char letterOfReturn[2][5] = {"Even", "Odd"};
    num = num>0 ? num : num*-1;
    char* answer = (char*)malloc(letterNumOfReturn[num%2]);
    strcpy(answer, letterOfReturn[num%2]);
    return answer;
}

1.13. 콜라츠 추측

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

int solution(int num) {
    int answer = 0;
    long long longNum = num;
    while(answer<=500) {
        if(longNum==1)
            return answer;
        
        if(longNum%2) {
            longNum = longNum*3+1;
            answer++;
        }
        else {
            longNum/=2;
            answer++;
        }   
    }
    return -1;
}

1.14. 평균 구하기

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

// arr_len은 배열 arr의 길이입니다.
double solution(int arr[], size_t arr_len) {
    double answer = 0;
    for(int i=0; i<arr_len; i++)
        answer+=arr[i];
    answer/=arr_len;
    return answer;
}

1.15. 하샤드 수

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

bool solution(int x) {
    bool answer = true;
    int sum=0;
    int temp;
    
    temp = x;
    while(temp!=0) {
        sum+=temp%10;   //각 자리 수를 더함
        temp/=10;
    }
    
    if(x%sum)   //원래 수가 각 자리 수를 더한 값으로 나누어 떨어지지 않으면 false
        answer = false;
    
    return answer;
}

1.16. 핸드폰 번호 가리기

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

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

1.17. 직사각형 별찍기

#include <stdio.h>

int main(void) {
    int a;
    int b;
    int i, j;
    
    scanf("%d %d", &a, &b);
    //printf("%d", a + b);
    
    for(i=0; i<b; i++) {
        for(j=0; j<a; j++) {
            printf("*");
        }
        printf("\n");
    }
    return 0;
}

2. Level 2

2.1. 주식가격

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

// prices_len은 배열 prices의 길이입니다.
int* solution(int prices[], size_t prices_len) {
    // return 값은 malloc 등 동적 할당을 사용해주세요. 할당 길이는 상황에 맞게 변경해주세요.
    int* answer = (int*)malloc(sizeof(int) * prices_len);
    for(int i = 0; i<prices_len; i++) {
        answer[i] = 0;
        for(int j = i+1; j<prices_len; j++) {
            answer[i]++;
            if(prices[j]<prices[i])
                break;
        }
    }
    return answer;
}
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-16 14:07:54
Processing time 0.0153 sec