U E D R , A S I H C RSS

새싹교실/2020/이찌반/전성수 (rev. 1.20)

새싹교실/2020/이찌반/전성수


1. 코테 과제 1단계

1.1. 약수의 합

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

int solution(int n) {
    int answer = 0;
    for (int i = n; i > 0; i--) {
        if (n % i == 0) {
            answer += i;
        }
        else continue;
    }
    return answer;
}

1.2. 하샤드 수

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

bool solution(int x) {
    bool answer = true;
    int sum = 0;
    while (1) {
        sum += x % 10;
        if (x / 10 < 10) {
            sum += x / 10;
            break;
        }
        else {
            x /= 10;
        }
    }
    if (x % sum == 0) answer = true;
    else  answer = false;

    return answer;
}

1.3. 수박수박수박수?

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

char* solution(int n) {
    char* answer = (char*)malloc(n * 3);
    char* a = "수";
    char* b = "박";
    for (int i = 0; i < n; i++) {
        strcpy(&answer[i * 3], i % 2 ? b : a);
    }

    return answer;
}

1.4. 두개 뽑아서 더하기

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#define SWAP(a, b) {int t = a; a = b; b = t;}

int* solution(int numbers[], size_t numbers_len) {
    int length = 0;
    int* answer = (int*)malloc(sizeof(int) * 100);
    for (int i = 0; i < numbers_len - 1; i++) {
        for (int j = i + 1; j < numbers_len; j++) {
            int add = numbers[i] + numbers[j];
            int check = 1;
            for (int k = 0; k < length; k++) {
                if (answer[k] == add) check = 0;
            }
            if (check == 1) {
                answer[length] = add;
                length++;
            }
        }
    }
    for (int i = 0; i < length; i++) {
        for (int j = 0; j < length - 1; j++) {
            if (answer[j] > answer[j + 1]) SWAP(answer[j], answer[j + 1]);
        }
    }
    return answer;
}

1.5. 별찍기

#include <stdio.h>

int main(void) {
    int a;
    int b;
    scanf_s("%d %d", &a, &b);
    printf("%d\n", a + b);
    for (int i = 0; i < b; i++) {

        for (int j = 0; j < a; j++) {
            printf("*");
        }

        printf("\n");
    }
    return 0;
}

1.6. 문자열 정렬

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#define SWAP(a,b) {int t = a;a = b;b = t;}
char* solution(const char* s) {
    int cnt = strlen(s);
    char* answer = (char*)malloc(sizeof(char) * cnt);
    for (int i = 0; i <= cnt; i++) {
        answer[i] = s[i];
    }
    for (int i = 0; i < cnt; i++) {
        for (int j = 0; j < cnt - 1; j++) {
            if (answer[j] <= answer[j + 1]) {
                SWAP(answer[j], answer[j + 1]);
            }
        }
    }
    return answer;
}

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

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

int solution(const char* s) {
    int answer = 0;
    answer = atoi(s);
    return answer;
}

1.8. 문자열 다루기

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

bool solution(const char* s) {
    bool answer = true;
    int count = strlen(s);

    for (int i = 0; i < count; i++) {
        if (isalpha(s[i])) {
            answer = false;
        }
    }
    if ((count == 4 || count == 6) != 1) {
        answer = false;
    }

    return answer;
}

1.9. 두 정수 사이의 합

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

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

1.10. 가운데 글자 가져오기

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

char* solution(const char* s) {
    char* answer;
    int cnt = strlen(s);
    if (cnt % 2) {
        answer = (char*)malloc(sizeof(char));
        answer[0] = s[cnt / 2];
        answer[1] = '\0';
    }
    else {
        answer = (char*)malloc(sizeof(char) * 2);
        answer[0] = s[(cnt / 2) - 1];
        answer[1] = s[cnt / 2];
        answer[2] = '\0';
    }
    return answer;
}

1.11. 2016년

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
char* solution(int a, int b) {
    char* answer = (char*)malloc(sizeof(char) * 10);
    int day = b;
    int month[] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    for (int i = 0; i < a; i++) {
        day += month[i - 1];
    }
    switch (day % 7) {
    case 0: answer = "THU"; break;
    case 1: answer = "FRI"; break;
    case 2: answer = "SAT"; break;
    case 3: answer = "SUN"; break;
    case 4: answer = "MON"; break;
    case 5: answer = "TUE"; break;
    case 6: answer = "WED"; break;
    }
    return answer;
}

1.12. 자릿수 더하기

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

int solution(int n) {
    int answer = 0;
    while (1) {
        answer += n % 10;
        n /= 10;
        if (n / 10 == 0) {
            answer += n;
            break;
        }

    }
    return answer;
}

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

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

int* solution(long long n) {
    int i = 0;
    int* answer = (int*)malloc(sizeof(int) * 11);
    do {
        answer[i++] = n % 10;
        n /= 10;
    } while (n > 0);

    return answer;
}

1.14. 정수 제곱근 판별

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

long long solution(long long n) {
    long long answer = 0;
    if ((int)sqrt(n) == sqrt(n)) {
        answer = (sqrt(n) + 1) * (sqrt(n) + 1);
    }
    else answer = -1;
    return answer;
}

1.15. 짝수와 홀수

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

char* solution(int num) {
    char* answer;
    if(num %2) {
        answer = (char*)malloc(sizeof(char)*3);
        answer = "Odd";
    }
    else {
        answer = (char*)malloc(sizeof(char)*4);
        answer = "Even";
    }
    return answer;
}

1.16. 콜라츠 추측

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

int solution(int num) {
    unsigned long long tmp = num;
    int answer = 0;
    for(answer = 0;;answer++) {
        if(tmp%2) {
            if(tmp == 1) break;
            tmp = (tmp*3) + 1;
        }
        else {
            tmp /= 2;
        }
    }
    if(answer >= 500) answer = -1;
    return answer;
}

1.17. 평균 구하기

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

double solution(int arr[], size_t arr_len) {
    double answer = 0;
    int sum = 0;
    for(int i = 0;i<arr_len;i++) {
        sum += arr[i];
    }
    answer = (double)sum/arr_len;
    return answer;
}

1.18. 핸드폰 번호 가리기

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

char* solution(const char* phone_number) {
    char* answer = (char*)malloc(sizeof(char)*strlen(phone_number)+1);
    strcpy(answer, phone_number);
    for(int i = 0;i<strlen(phone_number)-4;i++) {
        answer[i]='*';
    }
    return answer;
}

1.19. 이상한 문자 만들기

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


char* solution(const char* s) {
    char* answer = (char*)malloc(sizeof(char)*strlen(s)+1);
    int i = 0, index = 0;
    strcpy(answer, s);
    while(answer[index] != '\0'){
        if(answer[index] == ' ') {
            i = 1;
        }
        else if((i%2 == 0) && (answer[index] > 96)) {
            answer[index] -= 32;
        } 
        else if((i%2 == 1) && (answer[index] < 91)) {
            answer[index] += 32;
        }
        i++;
        index++;
    }
    return answer;
}

2. 코테 과제 2단계

2.1. 소수 만들기

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
bool checkprime(int num) {
    int i = num / 2;
    while(i > 1) {
        if(num % i == 0) return false;
        else i--;
    }

    return true;
}
int solution(int nums[], size_t nums_len) {
    int answer = 0;
    for(int i=0;i<nums_len-2;i++) {
        for(int j=i+1;j<nums_len-1;j++) {
            for(int k=j+1;k<nums_len;k++) {

                if(checkprime(nums[i]+nums[j]+nums[k])) answer++;
            }
        }
    }
    return answer;
}

2.2. 피보나치 수

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

int solution(int n) {
    if(n == 0) return 0;
    else if(n == 1 || n == 2) return 1;
    else return (solution(n-1) + solution(n-2)) % 1234567;
}

2.3. N개의 최소공배수

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

int gcd(int a, int b) {
    int result = 1;
    for(int i = 1; i <= a;i++) {
        if((a % i == 0)&&(b % i == 0)){
            result = i;
        }
    }
    return result;
}
int lcm(int a, int b) {
    return a*b/gcd(a,b);
}

int solution(int arr[], size_t arr_len) {
    long long answer = 0;
    answer = arr[0];
    for (int j = 1; j < arr_len; j++)
    {
        answer = lcm(arr[j], answer);
    }
    
    return answer;
}

2.4. 최솟값 만들기

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#define SWAP(a,b) {int t=a;a=b;b=t;}

int solution(int A[], size_t A_len, int B[], size_t B_len) {
    int answer = 0;
    int sum = 0;
    for(int i = 0;i < A_len;i++) {
        for (int j = 0; j < A_len-1;j++) {
            if(A[j] > A[j+1]) SWAP(A[j], A[j+1]);
        }
    }
    for(int i = 0;i < B_len;i++) {
        for (int j = 0; j < B_len-1;j++) {
            if(B[j] < B[j+1]) SWAP(B[j], B[j+1]);
        }
    }
    for(int i = 0; i < A_len;i++) {
        answer += A[i]*B[i];
    }
    
    return answer;
}

2.5. 올바른 괄호

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

bool solution(const char* s) {
    bool answer = true;
    bool check = true;
    int len = strlen(s);
    int left = 0, right = 0;
    for(int i = 0; i < len; i++) {
        if(s[i] == '(') {
            left += 1;
        }
        else if(s[i] == ')') {
            right += 1;
        }
        if(left < right) answer = false;
        
    }
    if(s[0] == ')') answer = false;
    else if(s[len] == '(') answer = false;
    else if(left != right) answer = false;
    
    return answer;
}

2.6. 주식가격

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

int* solution(int prices[], size_t prices_len) {
    int* answer = (int*)malloc(sizeof(int)*prices_len);
    for(int i = 0; i < prices_len;i++) {
        int cnt = 0;
        
        for(int j = i+1;j < prices_len;j++) {
            if(prices[i] <= prices[j]) {
                cnt++;
                continue;
            }
            else {
                cnt++;
                break;
            }
        }
        answer[i] = cnt;
    }
    return answer;
}

2.7. 큰수만들기

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

char* solution(const char* number, int k) {
    int cnt = strlen(number);
    char* answer = (char*)malloc(cnt);
    
    strcpy(answer, number);
    for(int i = 0; i < k; i++) {
        int j = 0;
        for(;j < cnt-1;j++) {
            if(answer[j] < answer[j+1]) {
                break;
            }
        }
        for(;j < cnt-1;j++) {
            answer[j] = answer[j+1];
        }
        cnt -= 1;
        answer[cnt] = NULL;
    }
    
    return answer;
}
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-16 14:07:54
Processing time 0.0261 sec