가까운 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