
369게임
#include <stdio.h>
int solution(int order) {
int res = 0;
while(order)
{
int num = order % 10;
if(num == 3 || num == 6 || num == 9)
res++;
order /= 10;
}
return res;
}
가위 바위 보
#include <stdio.h>
#include <string.h>
int analyzer(int n)
{
if(n == '2')
return '0';
if(n == '0')
return '5';
return '2';
}
char* solution(const char* rsp) {
int rsp_len = strlen(rsp);
char* answer = (char*)malloc(rsp_len * sizeof(char));
int i = -1;
while(++i < rsp_len)
answer[i] = analyzer(rsp[i]);
answer[i] = '\0';
return answer;
}
대문자와 소문자
#include <stdio.h>
#include <string.h>
char* solution(const char* my_string) {
int string_len = strlen(my_string);
int index = -1;
char* answer = (char*)malloc((string_len + 1) * sizeof(char));
if (answer == NULL)
return NULL;
while(++index < string_len)
{
if(my_string[index] >= 'A' && my_string[index] <= 'Z')
answer[index] = my_string[index] + 32;
else
answer[index] = my_string[index] - 32;
}
answer[index + 1] = '\0';
return answer;
}
문자열 정렬하기 (1)
#include <stdio.h>
#include <string.h>
int* solution(const char* my_string) {
int str_len = strlen(my_string);
int num_len = 0;
int idx = -1;
for(int i = 0; i < str_len; i++)
if(my_string[i] >= '0' && my_string[i] <= '9')
num_len++;
int* answer = (int*)malloc(num_len * sizeof(int));
if(answer == NULL)
return NULL;
for(int i = 0; i < str_len; i++)
if(my_string[i] >= '0' && my_string[i] <= '9')
answer[++idx] = my_string[i] - 48;
for(int i = 0; i < num_len; i++)
for(int j = 0; j < num_len; j++)
if(answer[i] < answer[j])
{
char temp = answer[i];
answer[i] = answer[j];
answer[j] = temp;
}
return answer;
}
문자열 정렬하기 (2)
#include <stdio.h>
#include <string.h>
char* solution(const char* my_string) {
int str_len = strlen(my_string);
char* answer = (char*)malloc((str_len + 1) * sizeof(char));
if(answer == NULL)
return NULL;
answer[str_len] = '\0';
for(int i = 0; i < str_len; i++)
{
answer[i] = my_string[i];
if(my_string[i] >= 'A' && my_string[i] <= 'Z')
answer[i] = my_string[i] + 32;
}
for(int i = 0; i < str_len; i++)
for(int j = 0; j < str_len; j++)
if(answer[i] < answer[j])
{
char temp = answer[i];
answer[i] = answer[j];
answer[j] = temp;
}
return answer;
}
배열 회전시키기
#include <stdio.h>
#include <string.h>
int* solution(int numbers[], size_t numbers_len, const char* direction) {
int* answer = (int*)malloc(numbers_len * sizeof(int));
if(answer == NULL)
return NULL;
if(!strcmp(direction, "right"))
{
for(int i = 1; i < numbers_len; i++)
answer[i] = numbers[i - 1];
answer[0] = numbers[numbers_len - 1];
}
else
{
for(int i = 0; i < numbers_len - 1; i++)
answer[i] = numbers[i + 1];
answer[numbers_len - 1] = numbers[0];
}
return answer;
}
암호 해독
#include <stdio.h>
#include <string.h>
char* solution(const char* cipher, int code) {
int cipher_len = strlen(cipher);
int cipher_idx = code - 1;
int index = -1;
char* answer = (char*)malloc((cipher_len / code + 1) * sizeof(char));
if (answer == NULL)
return NULL;
while(cipher_idx < cipher_len)
{
answer[++index] = cipher[cipher_idx];
cipher_idx += code;
}
answer[index + 1] = '\0';
return answer;
}
약수 구하기
#include <stdio.h>
int* solution(int n) {
int cnt = 0;
int idx = 0;
for(int i = 1; i <= n; i++)
if(n % i == 0)
cnt++;
int* answer = (int*)malloc(cnt * sizeof(int));
if(answer == NULL)
return NULL;
for(int i = 1; i <= n; i++)
if(n % i == 0)
answer[idx++] = i;
return answer;
}
외계행성의 나이
#include <stdio.h>
char* solution(int age) {
int length = 0;
int temp = age;
int idx = -1;
while(temp)
{
temp /= 10;
length++;
}
char* answer = (char*)malloc((length + 1) * sizeof(char));
if(answer == NULL)
return NULL;
while(++idx < length)
{
answer[idx] = age % 10 + 97;
age /= 10;
}
answer[idx] = '\0';
for (int i = 0; i < length / 2; i++)
{
int temp = answer[i];
answer[i] = answer[length - i - 1];
answer[length - i - 1] = temp;
}
return answer;
}
인덱스 바꾸기
#include <stdio.h>
#include <string.h>
char* solution(const char* my_string, int num1, int num2) {
int str_len = strlen(my_string);
char* answer = (char*)malloc((str_len + 1) * sizeof(char));
if(answer == NULL)
return NULL;
strcpy(answer, my_string);
char temp = answer[num1];
answer[num1] = answer[num2];
answer[num2] = temp;
return answer;
}
중복된 문자 제거
#include <stdio.h>
#include <string.h>
int checker(char str[], char c)
{
for(int idx = 0; idx < strlen(str); idx++)
if(str[idx] == c)
return 0;
return 1;
}
char* solution(const char* my_string) {
char* answer = (char*)malloc(strlen(my_string) * sizeof(char));
int ans = -1;
answer[++ans] = my_string[0];
for(int idx = 1; idx < strlen(my_string) + 1; idx++)
{
if(checker(answer, my_string[idx]))
answer[++ans] = my_string[idx];
answer[ans + 1] = '\0';
}
answer[ans] = '\0';
return answer;
}
직삼각형 출력하기
#include <stdio.h>
int main(void) {
int n;
scanf("%d", &n);
for(int i=0; i<n; i++)
{
for(int j=0; j<i+1; j++)
printf("*");
printf("\n");
}
return 0;
}
최댓값 만들기 (2)
#include <stdio.h>
int solution(int numbers[], size_t numbers_len) {
int answer = numbers[0] * numbers[1];
for (int i = 0; i < numbers_len; i++)
for(int j = 0; j < numbers_len; j++)
if(numbers[i] * numbers[j] > answer && i != j)
answer = numbers[i] * numbers[j];
return answer;
}
합성수 찾기
#include <stdio.h>
int composite(int n)
{
int cnt = 0;
for(int i = 1; i <= n; i++)
if(n % i == 0)
cnt++;
return cnt;
}
int solution(int n) {
int res = 0;
int idx = 0;
while(++idx <= n)
if(composite(idx) > 2)
res++;
return res;
}
n의 배수 고르기
#include <stdio.h>
int counter(int n, int numlist[], int numlist_len)
{
int index = -1;
int count = 0;
while(++index < numlist_len)
if(!(numlist[index] % n))
count++;
return count;
}
int* solution(int n, int numlist[], size_t numlist_len) {
int* answer = (int*)malloc(counter(n, numlist, numlist_len) * sizeof(int));
int index = -1;
int space = -1;
while(++index < numlist_len)
if(!(numlist[index] % n))
answer[++space] = numlist[index];
return answer;
}
https://github.com/JenSeop/Programmers-ICT/tree/main/Chap.04


