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