byidev.com
Study/Algorithm (10)
2024-04-18 00:07:44

날짜 비교하기

#include <stdio.h>

int solution(int d1[], size_t d1_l, int d2[], size_t d2_l) {
    return (d1[0] < d2[0]) || (d1[0] == d2[0]) && (d1[1] < d2[1]) || (d1[1] == d2[1]) && (d1[2] < d2[2] ? 1 : 0);
}

 

뒤에서 5등 위로

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

int* solution(int num[], size_t num_len) {
    int* res = (int*)malloc((num_len - 5) * sizeof(int));
    
    for(int i = 0; i < num_len; i++)
        for(int j = 0; j < num_len - i - 1; j++)
            if(num[j] > num[j + 1])
            {
                int temp = num[j];
                num[j] = num[j + 1];
                num[j + 1] = temp;
            }
    
    for(int i = 0; i < num_len - 5; i++)
        res[i] = num[i + 5];
    
    return res;
}

 

문자열 정수의 합

#include <stdio.h>

int solution(const char* num) {
    int res = 0;
    
    for(int idx = 0; num[idx]; idx++)
        res += num[idx] - 48;
    
    return res;
}

 

문자열이 몇 번 등장하는지 세기

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

int solution(const char* str, const char* pat) {
    int res = 0;
    
    for(int idx = 0; str[idx]; idx++)
    {
        int pdx = 0;
        if(str[idx] == pat[0])
            for(pdx; pat[pdx]; pdx++)
                if(str[idx + pdx] != pat[pdx])
                    break;
        if(pdx == strlen(pat))
            res++;
    }
    
    return res;
}

 

배열의 길이에 따라 다른 연산하기

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

#define OEC(l) ((l) % 2 == 0 ? 1 : 0)
#define ODD(i, n) ((i) % 2 != 0 ? n : 0)
#define EVE(i, n) ((i) % 2 == 0 ? n : 0)

int* solution(int arr[], size_t len, int n) {
    int* res = (int*)malloc(len * sizeof(int));
    
    for(int idx = 0; idx < len; idx++)
    {
        res[idx] = arr[idx];
        if(!OEC(len)) res[idx] = arr[idx] + EVE(idx, n);
        if(OEC(len)) res[idx] = arr[idx] + ODD(idx, n);
    }
    
    return res;
}

 

배열의 원소 삭제하기

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

int* solution(int a[], size_t a_len, int d[], size_t d_len) {
    int r_len = 0;
    
    for(int idx = 0; idx < a_len; idx++)
        for(int ddx = 0; ddx < d_len; ddx++)
            if(a[idx] == d[ddx])
            {
                a[idx] = 0;
                r_len++;
            }
    
    int* res = (int*)malloc((a_len - r_len) * sizeof(int));
    int rdx = 0;
    
    for(int idx = 0; idx < a_len; idx++)
        if(a[idx])
            res[rdx++] = a[idx];
    
    return res;
}

 

배열의 원소만큼 추가하기

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

int res_len(int arr[], int len)
{
    int res = 0;
    
    for(int idx = 0; idx < len; idx++)
        res += arr[idx];
    
    return res;
}

int* solution(int arr[], size_t arr_len) {
    int* res = (int*)malloc(res_len(arr, arr_len) * sizeof(int));
    int rpc = 0;
    
    for(int idx = 0; idx < arr_len; idx++)
        for(int rdx = 0; rdx < arr[idx]; rdx++)
            res[rpc++] = arr[idx];
    
    return res;
}

 

조건에 맞게 수열 변환하기 2

#include <stdio.h>

#define CAL(n) ((n) >= 50 ? ((n) % 2 == 0 ? (n / 2) : (n)) : ((n) % 2 ? (n * 2 + 1) : (n)))

int solution(int arr[], size_t arr_len) {
    int res = 0;
    
    while(1)
    {
        int bef = 0, aft = 0;
        for(int idx = 0; idx < arr_len; idx++)
        {
            bef += arr[idx];
            arr[idx] = CAL(arr[idx]);
            aft += arr[idx];
        }
        if ((bef - aft) == 0) break;
        res++;
    }
    
    return res;
}

 

조건에 맞게 수열 변환하기 3

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

int* solution(int arr[], size_t len, int k) {
    int* res = (int*)malloc(len * sizeof(int));
    
    for(int idx = 0; idx < len; idx++)
        res[idx] = (k % 2) ? (arr[idx] * k) : (arr[idx] + k);
    
    return res;
}

 

주사위 게임 1

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

#define POW(a, b) ((a) * (a) + (b) * (b))

int solution(int a, int b) {
    if(a % 2 && b % 2)
        return POW(a, b);
    if((a % 2 && b % 2 == 0) || (a % 2 == 0 && b % 2))
        return 2 * (a + b);
    return abs(a - b);
}

 

커피 심부름

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

#define AME(n) ((n) ? 4500 : 0)
#define CAF(n) ((n) ? 5000 : 0)

int solution(const char* order[], size_t order_len) {
    int res = 0;
    
    for(int idx = 0; idx < order_len; idx++)
        res += AME(strstr(order[idx], "ame")) + CAF(strstr(order[idx], "caf")) + AME(strstr(order[idx], "any"));
    
    return res;
}

 

콜라츠 수열 만들기

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

#define COL(n) ((n) % 2 ? 3 * n + 1 : n / 2) 

int* solution(int n) {
    int* res = (int*)malloc((n - 3) * sizeof(int));
    
    res[0] = n;
    for(int idx = 1; n != 1; idx++)
    {
        n = COL(n);
        res[idx] = n;
    }
    
    return res;
}

 

X 사이의 개수

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

int res_len(char* str)
{
    int res = 1;
    
    for(int idx = 0; idx < strlen(str); idx++)
        if(str[idx] == 'x')
            res++;
    
    return res;
}

int* solution(const char* str) {
    int* res = (int*)calloc(res_len(str), sizeof(int));
    int rdx = 0;
    
    for(int idx = 0; idx < strlen(str); idx++)
    {
        if(str[idx] != 'x') res[rdx]++;
        else rdx++;
    }

    return res;
}

 

https://github.com/JenSeop/Programmers-C/tree/main/Chap.09

2024-04-08 00:36:06

 

0 떼기

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

char* solution(const char* str) {
    char* res = (char*)malloc((strlen(str) + 1) * sizeof(char));
    int rdx = 0;
    int flg = 0;
    
    for(int idx = 0; idx < strlen(str); idx++)
    {
        if(str[idx] != 48)
            flg = 1;
        if(flg)
            res[rdx++] = str[idx];
    }
    res[rdx] = NULL;
    
    return res;
}

 

1로 만들기

#include <stdio.h>

#define CAL(n) ((n) % 2 == 0 ? (n) / 2 : ((n) - 1) / 2)

int solution(int num[], size_t len) {
    int res = 0;
    
    for(int idx = 0; idx < len; idx++)
        for(;num[idx];res++)
            num[idx] = CAL(num[idx]);
    
    return res - len;
}

 

간단한 식 계산하기

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

#define SIG(c) (((c) == '+' || (c) == '-' || (c) == '*') ? 1 : 0)
#define STP(c) ((c) == '+' ? 1 : (c) == '-' ? -1 : (c) == '*' ? 0 : 0)

int sig_find(char* str)
{
    for(int idx = 0; str[idx]; idx++)
        if(SIG(str[idx]))
            return idx;
    return 0;
}

int solution(const char* str) {
    int sig_spc = sig_find(str);
    int sig_flg = STP(str[sig_spc]);
    char* left[sig_spc];
    char* right[sig_spc];
    
    strncpy(left, str, sig_spc);
    left[sig_spc - 1] = NULL;
    strncpy(right, str + sig_spc + 2, strlen(str));
    right[sig_spc - 1] = NULL;
    
    if(sig_flg)
        return atoi(left) + atoi(right) * sig_flg;
    else
        return atoi(left) * atoi(right);
    return 0;
}

 

문자열 바꿔서 찾기

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

#define TRS(c) ((c) == 65 ? 66 : 65)

int solution(const char* str, const char* pat) {
    char *cpy = str;
    
    for(int idx = 0; cpy[idx]; idx++)
        cpy[idx] = TRS(cpy[idx]);
    
    return strstr(cpy, pat) ? 1 : 0;
}

 

문자열을 정수로 변환하기

#include <stdio.h>

int solution(const char* str) {
    int res = str[0] - 48;
    
    for(int idx = 1; idx < strlen(str); idx++)
        res = (res * 10) + str[idx] - 48;
    
    return res;
}

 

배열 만들기 1

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

int* solution(int n, int k) {
    int* res = (int*)malloc((n / k) * sizeof(int));
    
    for(int idx = 0; idx < (n / k); idx++)
        res[idx] = (idx + 1) * k;
    
    return res;
}

 

부분 문자열

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

int solution(const char* str1, const char* str2) {
    return (bool)strstr(str2, str1);
}

 

부분 문자열인지 확인하기

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

int solution(const char* str1, const char* str2) {
    return strstr(str1, str2) ? 1 : 0;
}

 

원하는 문자열 찾기

#include <stdio.h>

#define alp(c) ((c) < 91 ? (c + 32) : (c))

int solution(const char* str, const char* pat) {
    int flg = 0;
    
    for(int idx = 0; idx < strlen(str); idx++)
    {
        if(alp(str[idx]) == alp(pat[flg]))
            flg++;
        else
            flg = 0;
        if(flg == strlen(pat))
            return 1;
    }       
    return 0;
}

 

카운트 다운

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

int* solution(int start, int end) {
    int* res = (int*)malloc((start - end) * sizeof(int));
    int rdx = 0;
    
    for(start; start >= end; start--)
        res[rdx++] = start;
    
    return res;
}

 

특정한 문자를 대문자로 바꾸기

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

#define TRS(c, t) ((c) == (t) ? (c - 32) : (c))

char* solution(const char* str, const char* t) {
    char* res = (char*)malloc((strlen(str) + 1) * sizeof(char));
    
    for(int idx = 0; idx < strlen(str); idx++)
        res[idx] = TRS(str[idx], t[0]);
    res[strlen(str)] = NULL;
    
    return res;
}

 

홀수 vs 짝수

#include <stdio.h>

#define EORO(n) ((n) % 2 == 0 ? 1 : 0)

int solution(int num[], size_t len) {
    int res = 0, eve = 0, odd = 0;
    
    for(int idx = 0; idx < len; idx++)
    {
        if(EORO(idx + 1)) eve += num[idx];
        else odd += num[idx];
    }
    
    return eve > odd ? eve : odd;
}

 

A 강조하기

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

#define TRS(c) ((c) == 97 ? -32 : (c) < 91 && (c) > 65 ? 32 : 0)

char* solution(const char* str) {
    char* res = (char*)malloc((strlen(str) + 1) * sizeof(char));
    
    for(int idx = 0; idx < strlen(str); idx++)
        res[idx] = str[idx] + TRS(str[idx]);
    res[strlen(str)] = NULL;
    
    return res;
}

 

l로 만들기

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

#define TRS(c) ((c) < 108 ? 108 : (c))

char* solution(const char* str) {
    char* res = (char*)malloc((strlen(str) + 1) * sizeof(char));
    
    for(int idx = 0; idx < strlen(str); idx++)
        res[idx] = TRS(str[idx]);
    res[strlen(str)] = NULL;
    
    return res;
}

 

n보다 커질 때까지 더하기

#include <stdio.h>

#define CHK(res, num) ((res) > (num) ? 0 : 1)

int solution(int num[], size_t num_len, int n) {
    int res = 0;
    
    for(int idx = 0; CHK(res, n); idx++)
        res += num[idx];
    
    return res;
}

 

rny_string

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

int m_counter(char* str)
{
    int cnt = 0;
    
    for(int idx = 0; idx < strlen(str); idx++)
        if(str[idx] == 'm')
            cnt++;
    
    return cnt;
}

char* solution(const char* str) {
    char* res = (char*)malloc((strlen(str) + m_counter(str) + 1) * sizeof(char));
    int spc = 0;
    
    for(int idx = 0; idx < strlen(str); idx++)
    {
        if(str[idx] == 'm')
        {
            res[idx + spc] = 'r';
            res[idx + spc + 1] = 'n';
            spc++;
        }
        else
            res[idx + spc] = str[idx];
    }
    res[strlen(str) + m_counter(str)] = NULL;
        
    return res;
}

 

https://github.com/JenSeop/Programmers-C/tree/main/Chap.08

2024-04-02 08:00:03

 

가까운 1 찾기

#include <stdio.h>

int solution(int arr[], size_t len, int idx) {
    for (int i = 0; i < len; i++)
        if(arr[i] && i >= idx)
            return i;
    return -1;
}

 

길이에 따른 연산

#include <stdio.h>

int sum_all(int num[], int len)
{
    int res = 0;
    
    for(int idx = 0; idx < len; idx++)
        res += num[idx];
    
    return res;
}

int mul_all(int num[], int len)
{
    int res = 1;
    
    for(int idx = 0; idx < len; idx++)
        res *= num[idx];
    
    return res;
}

int solution(int num[], size_t len) {
    if(len > 10)
        return sum_all(num, len);
    return mul_all(num, len);
}

 

대문자로 바꾸기

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

#define TRS(c) ((c) > 96 && (c) < 123 ? -32 : 0)

char* solution(const char* str) {
    char* res = (char*)malloc((strlen(str) + 1) * sizeof(char));
    
    for(int idx = 0; idx < strlen(str); idx++)
        res[idx] = str[idx] + TRS(str[idx]);
    res[strlen(str)] = '\0';
    
    return res;
}

 

문자 개수 세기

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

int* solution(const char* str) {
    int* res = (int*)calloc(52, sizeof(int));
    
    while(*str)
    {
        if(*str >= 'A' && *str <= 'Z')
            res[*str - 'A']++;
        else
            res[*str - 'A' - 6]++;
        str++;
    }
        
    return res;
}

 

문자열로 변환

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

char* solution(int n) {
    char* res = (char*)malloc(6 * sizeof(char));
    int rdx = 0;
    int lst[5] = {0, };
    int ldx = 0;
    
    while(n)
    {
        lst[ldx++] = n % 10;
        n /= 10;
    }
    
    for(int idx = ldx - 1; idx >= 0; idx--)
        res[rdx++] = lst[idx] + 48;
    res[ldx] = NULL;
    
    return res;
}

 

배열 비교하기

#include <stdio.h>

#define COMP(x, y) ((x) > (y) ? 1 : (x) < (y) ? -1 : (x) == (y) ? 0 : 0)

int sum(int arr[], int len)
{
    int res = 0;
    
    for(int idx = 0; idx < len; idx++)
        res += arr[idx];
    
    return res;
}

int solution(int arr1[], size_t arr1_len, int arr2[], size_t arr2_len) {
    if(arr1_len - arr2_len)
        return COMP(arr1_len, arr2_len);
    return COMP(sum(arr1, arr1_len), sum(arr2, arr2_len));
}

 

세로 읽기

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

char* solution(const char* str, int rows, int cols) {
    char* res = (char*)malloc((strlen(str) + 1) * sizeof(char));
    int spc = 0;
    
    for(int idx = cols - 1; idx < strlen(str); idx += rows)
        res[spc++] = str[idx];
    res[spc] = NULL;
    
    return res;
}

 

소문자로 바꾸기

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

#define LOW(n) ((n) < 91 ? (n + 32) : (n))

char* solution(const char* str) {
    char* res = (char*)malloc((strlen(str) + 1) * sizeof(char));
    
    for(int idx = 0; idx < strlen(str); idx++)
        res[idx] = LOW(str[idx]);
    res[strlen(str)] = NULL;
    
    return res;
}

 

수 조작하기 1

#include <stdio.h>

#define CONTROL(x) ((x == 'w') ? 1 : (x == 's') ? -1 : (x == 'd') ? 10 : (x == 'a') ? -10 : 0)

int solution(int n, const char* control) {
    for(int idx = 0; idx < strlen(control); idx++)
        n += CONTROL(control[idx]);
    return n;
}

 

수 조작하기 2

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

char* solution(int log[], size_t len) {
    char* res = (char*)malloc((len) * sizeof(char));
    int spc = 0;
    
    for (int idx = 1; idx < len; idx++)
    {
        int diff = log[idx] - log[idx - 1];
        switch(diff)
        {
            case -10: res[spc++] = 'a'; break;
            case -1: res[spc++] = 's'; break;
            case 1: res[spc++] = 'w'; break;
            case 10: res[spc++] = 'd'; break;
        }
    }
    res[spc] = NULL;
    
    return res;
}

 

정수 부분

#include <stdio.h>

int solution(double flo) {
    return (int)flo;
}

 

정수 찾기

#include <stdio.h>

int solution(int num[], size_t len, int n) {
    for(int idx = 0; idx < len; idx++)
        if(num[idx] == n)
            return 1;
    return 0;
}

 

첫 번째로 나오는 음수

#include <stdio.h>

int solution(int num[], size_t len) {
    for(int idx = 0; idx < len; idx++)
        if(num[idx] < 0)
            return idx;
    return -1;
}

 

n 번째 원소부터

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

int* solution(int num[], size_t len, int n) {
    int* res = (int*)malloc((len - (n - 1)) * sizeof(int));
    int spc = 0;
    
    for(int idx = n - 1; idx < len; idx++)
        res[spc++] = num[idx];
    
    return res;
}

 

n 번째 원소까지

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

int* solution(int num[], size_t len, int n) {
    int* res = (int*)malloc(n * sizeof(int));
    
    for(int idx = 0; idx < n; idx++)
        res[idx] = num[idx];
    
    return res;
}

 

qr code

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

#define QR(q, r, c) ((c) % (q) == (r))

char* solution(int q, int r, const char* code) {
    char* res = (char*)malloc((strlen(code) + 1) * sizeof(char));
    int rdx = 0;
    
    for(int idx = 0; idx < strlen(code); idx++)
        if(QR(q, r, idx))
            res[rdx++] = code[idx];
    res[rdx] = NULL;
    
    return res;
}

 

https://github.com/JenSeop/Programmers-C/tree/main/Chap.07

2024-03-27 18:28:41

 

7의 개수

#include <stdio.h>

#define TARGET 7

int counter(int n)
{
    int cnt = 0;
    
    while(n)
    {
        if(n % 10 == 7)
            cnt++;
        n /= 10;
    }
    
    return cnt;
}

int solution(int arr[], size_t len) {
    int res = 0;
    int idx = -1;
    
    while(++idx < len)
        if(counter(arr[idx]))
            res += counter(arr[idx]);
            
    return res;
}

 

가까운 수

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

int solution(int arr[], size_t len, int n) {
    int res = arr[0];
    int min_diff = abs(arr[0] - n);
    
    for (int i = 1; i < len; i++)
    {
        int diff = abs(arr[i] - n);
        if(diff < min_diff || (diff == min_diff && arr[i] < res))
        {
            res = arr[i];
            min_diff = diff;
        }
    }
    
    return res;
}

 

간단한 논리 연산

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

#define CJT(x, y) ((x) || (y))
#define ITS(x, y) ((x) && (y))

bool solution(bool x1, bool x2, bool x3, bool x4) {
    return (ITS(CJT(x1, x2), CJT(x3, x4)));
}

 

구슬을 나누는 경우의 수

#include <stdio.h>

int solution(int balls, int share) {
    if (share == 0 || balls == share)
        return 1;
    else
        return solution(balls - 1, share - 1) + solution(balls - 1, share);
}

 

글자 이어 붙여 문자열 만들기

#include <stdio.h>

char* solution(const char* str, int idx_lst[], size_t idx_lst_len) {
    char* res = (char*)malloc((idx_lst_len + 1) * sizeof(char));
    
    for(int idx = 0; idx < idx_lst_len; idx++)
        res[idx] = str[idx_lst[idx]];
    res[idx_lst_len] = NULL;
    
    return res;
}

 

등차수열의 특정한 항만 더하기

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

int solution(int a, int d, bool inc[], size_t inc_len) {
    int res = 0;
    
    for(int idx = 0; idx < inc_len; idx++)
        if(inc[idx])
            res += (a + d * idx);
    
    return res;
}

 

마지막 두 원소

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

#define CHK(lst, bef) ((lst) > (bef) ? (lst - bef) : (lst * 2))

int* solution(int num[], size_t len) {
    int* res = (int*)malloc((len + 1) * sizeof(int));
    
    for(int idx = 0; idx < len; idx++)
        res[idx] = num[idx];
    res[len] = CHK(num[len - 1], num[len - 2]);
    
    return res;
}

 

모스 부호

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

char* cutter(const char* let, int st, int ed) {
    char* cut = (char*)malloc((ed + 1) * sizeof(char));
    
    strncpy(cut, let + st, ed);
    cut[ed] = NULL;
    
    return cut;
}

int find_ed(const char* let, int st)
{
    int idx = 1;
    
    let += st;
    while(let++)
    {
        if(*let == ' ')
            return idx;
        idx++;
    }
    
    return 0;
}

int match_morse(char* str)
{
    char* morse[] = {
        ".-","-...","-.-.","-..",".","..-.","--.","....","..",
        ".---","-.-",".-..","--","-.","---",".--.","--.-",".-.",
        "...","-","..-","...-",".--","-..-","-.--","--.."
        };
    
    for(int idx = 0; idx < 26; idx++)
        if(!strcmp(morse[idx], str))
            return idx + 97;
    
    return 0;
}

char* solution(const char* let) {
    char* res = (char*)malloc(strlen(let) * sizeof(char));
    int idx = -1;
    
    for(int st = 0; st < strlen(let); st += find_ed(let, st) + 1)
        res[++idx] = match_morse(cutter(let, st, find_ed(let, st)));
    res[++idx] = NULL;
    
    return res;
}

 

문자열 뒤집기

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

char* solution(const char* str, int s, int e) {
    char* res = (char*)malloc((strlen(str) + 1) * sizeof(char));
    int idx = s;
    
    strcpy(res, str);
    for(e; e >= s; e--)
        res[idx++] = str[e];
    res[strlen(str)] = NULL;
    
    return res;
}

 

문자열 뒤의 n글자

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

char* solution(const char* str, int n) {
    char* res = (char*)malloc((n + 1) * sizeof(char));
    int spc = 0;
    
    for(int idx = strlen(str) - n; idx < strlen(str); idx++)
        res[spc++] = str[idx];
    res[n] = '\0';
    
    return res;
}

 

문자열 앞의 n글자

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

char* solution(const char* str, int n) {
    char* res = (char*)malloc((n + 1) * sizeof(char));
    
    for(int idx = 0; idx < n; idx++)
        res[idx] = str[idx];
    res[n] = NULL;
    
    return res;
}

 

부분 문자열 이어 붙여 문자열 만들기

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

char* solution(const char* str[], size_t str_len, int** spc, size_t spc_row, size_t spc_col) {
    char* res = (char*)malloc((str_len * spc_row * spc_col + 1) * sizeof(char));
    int rdx = 0;
    
    for(int sdx = 0; sdx < str_len; sdx++)
        for(int pdx = spc[sdx][0]; pdx <= spc[sdx][1]; pdx++)
            res[rdx++] = str[sdx][pdx];
    res[rdx] = '\0';
    
    return res;
}

 

원소들의 곱과 합

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

int sum_pow_idx(int num[], int len)
{
    int res = 0;
    
    for(int idx = 0; idx < len; idx++)
        res += num[idx];
    
    return pow(res, 2);
}

int mul_all_idx(int num[], int len)
{
    int res = 1;
    
    for(int idx = 0; idx < len; idx++)
        res *= num[idx];
    
    return res;
}

int solution(int num[], size_t len) {
    if(mul_all_idx(num,len) < sum_pow_idx(num,len))
        return 1;
    return 0;
}

 

이어 붙인 수

#include <stdio.h>

#define EVE(n) ((n) % 2 == 0 ? n : 0)
#define ODD(n) ((n) % 2 != 0 ? n : 0)
#define MOV(n) ((n) ? 10 : 1)

int solution(int num[], size_t len) {
    int eve = 0, odd = 0;
    
    for(int idx = 0; idx < len; idx++)
    {
        eve = eve * MOV(EVE(num[idx])) + EVE(num[idx]);
        odd = odd * MOV(ODD(num[idx])) + ODD(num[idx]);
    }
    
    return eve + odd;
}

 

카운트 업

#include <stdio.h>

int* solution(int s_num, int e_num) {
    int* res = (int*)malloc((e_num - s_num + 1) * sizeof(int));
    int idx = 0;
    
    for (s_num; s_num <= e_num; s_num++)
        res[idx++] = s_num;
    
    return res;
}

 

https://github.com/JenSeop/Programmers-C/tree/main/Chap.06

2024-03-16 02:41:17

공배수

def solution(num, n, m):
    if num % n == 0 and num % m == 0:
        return 1
    else:
        return 0

 

대소문자 바꿔서 출력하기

str = input().swapcase()
print(str)

 

더 크게 합치기

def solution(a, b):
    ab = int(str(a)+str(b))
    ba = int(str(b)+str(a))
    return max(ab, ba)

 

덧셈식 출력하기

a, b = map(int, input().strip().split(' '))
print(f"{a} + {b} = {a+b}")

 

두 수의 연산값 비교하기

def solution(a, b):
    return max(int(str(a)+str(b)),2*a*b)

 

문자 리스트를 문자열로 반환하기

def solution(arr):
    res = ''
    
    for c in arr:
        res += c
    
    return res

 

문자열 겹쳐쓰기

def solution(str1, str2, s):
    return str1[:s] + str2 + str1[len(str2) + s:]

 

문자열 돌리기

str = input()
for c in str:
    print(c)

 

문자열 반복해서 출력하기

str, n = input().strip().split(' ')
n = int(n)
print(str * n)

 

문자열 붙여서 출력하기

str1, str2 = input().strip().split(' ')
print(str1+str2)

 

문자열 섞기

def solution(str1, str2):
    res = ""
    
    for s1, s2 in zip(str1, str2):
        res += s1 + s2
    
    return res

 

문자열 출력하기

str = input()
print(str)

 

조건 문자열

def solution(ineq, eq, n, m):
    if ineq == "<":
        if eq == "=":
            return int(n <= m)
        if eq == "!":
            return int(n < m)
    if ineq == ">":
        if eq == "=":
            return int(n >= m)
        if eq == "!":
            return int(n > m)

 

특수문자 출력하기

str = "!@#$%^&*(\\\'\"<>?:;"
print(str)

 

홀짝 구분하기

a = int(input())
if a % 2:
    print(f"{a} is odd")
else:
    print(f"{a} is even")

 

홀짝에 따라 다른 값 반환하기

def odd(n):
    return sum(i for i in range(1, n+1, 2))

def even(n):
    return sum(i**2 for i in range(2, n+1, 2))

def solution(n):
    if n % 2 == 0:
        return even(n)
    else:
        return odd(n)

 

a 와 b 출력하기

# input() 입력
# strip() 문자열 앞 뒤 공백 제거
# split('') 문자열 공백 기준으로 나눔
a, b = map(int, input().strip().split(' '))
print(f'a = {a}')
print(f'b = {b}')

 

flag에 따라 다른 값 반환하기

def solution(a, b, flag):
    if flag == 1:
        return a+b
    else:
        return a-b

 

n의 배수

def solution(num, n):
    if(num%n):
        return 0
    else:
        return 1

 

https://github.com/JenSeop/Programmers-Python/tree/main/Chap.01

2024-03-15 01:50:53

 

등수 매기기

#include <stdio.h>

int avg(int** score, int row)
{
    return score[row][0] + score[row][1];
}

int* solution(int** score, size_t row, size_t col) {
    int* res = (int*)malloc(row * sizeof(int));
    
    for(int i = 0; i < row; i++)
    {
        int now = avg(score, i);
        
        res[i] = 1;
        for(int j = 0; j < row; j++)
            if(now < avg(score, j))
                res[i]++;
    }
        
    return res;
}

 

로그인 성공

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

char* solution(const char* id_pw[], size_t id_pw_len, const char*** db, size_t db_rows, size_t db_cols) {
    int flg = 0;
    
    for(int i = 0; i < db_rows; i++)
        if(!strcmp(id_pw[0], db[i][0]))
        {
            flg = 1;
            if(!strcmp(id_pw[1], db[i][1]))
                flg = 2;
        }
    
    char* res = (char*)malloc(9 * sizeof(char));
    
    if(flg == 0)
        strcpy(res, "fail\0");
    if(flg == 1)
        strcpy(res, "wrong pw\0");
    if(flg == 2)
        strcpy(res, "login\0");
    
    return res;
}

 

문자열 계산하기

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

int solution(const char* str) {
    int res = 0;
    int idx = -1;
    int num = 0;
    int sign = 1;
    
    while(++idx < strlen(str))
    {
        if(str[idx] == '-' && sign > 0)
            sign *= -1;
        if(str[idx] == '+' && sign < 0)
            sign *= -1;
        if(str[idx] >= '0' && str[idx] <= '9')
            num = (num + str[idx] - '0') * 10;
        if(str[idx + 1] == ' ' || str[idx + 1] == NULL)
        {
            res = res + sign * (num / 10);
            num = 0;
        }
    }
    
    return res;
}

 

삼각형의 완성 조건 (2)

#include <stdio.h>

int tri_check(int a, int b, int c)
{
    if(a + b > c && a + c > b && b + c > a)
        return 1;
    
    return 0;
}

int solution(int sides[], size_t sides_len) {
    int cnt = 0;
    int idx = -1;
    int lmt = sides[0] + sides[1];
    
    while(++idx < lmt)
        if(tri_check(sides[0], sides[1], idx))
            cnt++;
    
    return cnt;
}

 

숨어있는 숫자의 덧셈 (2)

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

int solution(const char* my_string) {
    int res = 0;
    int num = 0;
    
    for(int i = 0; i < strlen(my_string) + 1; i++)
    {
        if(my_string[i] >= '0' && my_string[i] <= '9')
        {
            num += (my_string[i] - 48);
            num *= 10;
        }
        else
        {
            res += num / 10;
            num = 0;
        }
    }
    
    return res;
}

 

안전지대

#include <stdio.h>

int solution(int** dot, size_t row, size_t col) {
    int sqr[100][100] = {0, };
    int res = 0;
    
    for(int y = 0; y < row; y++)
        for(int x = 0; x < col; x++)
        {
            if(dot[x][y] == 1)
            {
                sqr[x][y] = 1;
                if(x > 0)
                    sqr[x - 1][y] = 1;
                if(x < col - 1)
                    sqr[x + 1][y] = 1;
                if(y > 0)
                    sqr[x][y - 1] = 1;
                if(y < row - 1)
                    sqr[x][y + 1] = 1;
                if(x > 0 && y > 0)
                    sqr[x - 1][y - 1] = 1;
                if(x < col - 1 && y > 0)
                    sqr[x + 1][y - 1] = 1;
                if(x > 0 && y < row - 1)
                    sqr[x - 1][y + 1] = 1;
                if(x < col - 1 && y < row - 1)
                    sqr[x + 1][y + 1] = 1;
            }
        }
    
    for(int y = 0; y < row; y++)
        for(int x = 0; x < col; x++)
            if(sqr[x][y])
                res++;
    
    return row * col - res;
}

 

영어가 싫어요

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

int str_to_int(char* numbers, int idx)
{
    if(numbers[idx] == 'z')
        return 0;
    if(numbers[idx] == 'o')
        return 1;
    if(numbers[idx] == 't')
    {
        if(numbers[idx + 1] == 'w')
            return 2;
        return 3;
    }
    if(numbers[idx] == 'f')
    {
        if(numbers[idx + 1] == 'o')
            return 4;
        return 5;
    }
    if(numbers[idx] == 's')
    {
        if(numbers[idx + 1] == 'i')
            return 6;
        return 7;
    }
    if(numbers[idx] == 'e')
        return 8;
    if(numbers[idx] == 'n')
        return 9;
    
    return -1;
}

int str_int_spc(int n)
{
    if(n == 1 || n == 2 || n == 6)
        return 3;
    if(n == 0 || n == 4 || n == 5 || n == 9)
        return 4;
    
    return 5;
}

long long solution(const char* numbers) {
    long long res = 0;
    int idx = -1;
    
    while(numbers[++idx])
        {
            int num = str_to_int(numbers, idx);
            idx = idx + str_int_spc(num) - 1;
            res = res * 10 + num;
        }
    
    return res;
}

 

종이 자르기

#include <stdio.h>

int solution(int M, int N) {
    return M * N - 1;
}

 

진료 순서 정하기

#include <stdio.h>

int* solution(int arr[], size_t len) {
    int* res = (int*)malloc(len * sizeof(int));
    
    for(int i = 0; i < len; i++)
        res[i] = len;
    
    for(int i = 0; i < len; i++)
        for(int j = 0; j < len; j++)
            if(i != j && arr[i] > arr[j])
                res[i]--;
    
    return res;
}

 

최빈값 구하기

#include <stdio.h>

int idx_cnt(int n, int array[], size_t array_len)
{
    int idx = -1;
    int cnt = 0;
    
    while(++idx < array_len)
        if(n == array[idx])
            cnt++;
    
    return cnt;
}

int solution(int array[], size_t array_len) {
    int cnt[101] = {0, };
    int res = 0;
    int max = 0;
    int max_idx = 0;
    
    if(array_len == 1)
        return array[0];
    
    for(int i = 1; i < array_len; i++)
    {
        cnt[i] = idx_cnt(i, array, array_len);
        if(max < cnt[i])
        {
            max = cnt[i];
            res = i;
        }
    }
    
    for(int i = 1; i < array_len; i++)
        if(max == cnt[i] && res != i)
            return -1;
    
    return res;
}

 

팩토리얼

#include <stdio.h>

int solution(int n) {
    int fac = 1;
    int idx = 0;
    
    while(++idx)
    {
        fac *= idx;
        if(fac > n)
            return idx - 1;
    }
}

 

A로 B만들기

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

int solution(const char* before, const char* after) {
    int bef[26] = {0, };
    int aft[26] = {0, };
    
    for(int i = 0; i < strlen(before); i++)
    {
        bef[before[i] - 97]++;
        aft[after[i] - 97]++;
    }
    
    for(int i = 0; i < 26; i++)
        if(bef[i] != aft[i])
            return 0;
    
    return 1;
}

 

K의 개수

#include <stdio.h>

int solution(int i, int j, int k) {
    int res = 0;
    int idx = i - 1;
    
    while(++idx <= j)
    {
        int temp = idx;
        while(temp)
        {
            if(temp % 10 == k)
                res++;
            temp /= 10;
        }
    }
    
    return res;
}

 

https://github.com/JenSeop/Programmers-ICT-C/tree/main/Chap.05