More actions
imported>stome6746 No edit summary |
(Repair batch-0006 pages from live compare) |
||
| (22 intermediate revisions by one other user not shown) | |||
| Line 1: | Line 1: | ||
__TOC__ | __TOC__ | ||
== 코테 과제 == | == 코테 과제 1단계 == | ||
=== 약수의 합 === | === 약수의 합 === | ||
#include <stdbool.h> | #include <stdbool.h> | ||
| Line 48: | Line 48: | ||
char* b = "박"; | char* b = "박"; | ||
for (int i = 0; i < n; i++) { | for (int i = 0; i < n; i++) { | ||
strcpy(&answer | strcpy(&answer[i * 3], i % 2 ? b : a); | ||
} | } | ||
| Line 58: | Line 58: | ||
#define SWAP(a, b) {int t = a; a = b; b = t;} | #define SWAP(a, b) {int t = a; a = b; b = t;} | ||
int* solution(int numbers | int* solution(int numbers[], size_t numbers_len) { | ||
int length = 0; | int length = 0; | ||
int* answer = (int*)malloc(sizeof(int) * 100); | int* answer = (int*)malloc(sizeof(int) * 100); | ||
for (int i = 0; i < numbers_len - 1; i++) { | for (int i = 0; i < numbers_len - 1; i++) { | ||
for (int j = i + 1; j < numbers_len; j++) { | for (int j = i + 1; j < numbers_len; j++) { | ||
int add = numbers | int add = numbers[i] + numbers[j]; | ||
int check = 1; | int check = 1; | ||
for (int k = 0; k < length; k++) { | for (int k = 0; k < length; k++) { | ||
if (answer | if (answer[k] == add) check = 0; | ||
} | } | ||
if (check == 1) { | if (check == 1) { | ||
answer | answer[length] = add; | ||
length++; | length++; | ||
} | } | ||
| Line 76: | Line 76: | ||
for (int i = 0; i < length; i++) { | for (int i = 0; i < length; i++) { | ||
for (int j = 0; j < length - 1; j++) { | for (int j = 0; j < length - 1; j++) { | ||
if (answer | if (answer[j] > answer[j + 1]) SWAP(answer[j], answer[j + 1]); | ||
} | } | ||
} | } | ||
| Line 107: | Line 107: | ||
char* answer = (char*)malloc(sizeof(char) * cnt); | char* answer = (char*)malloc(sizeof(char) * cnt); | ||
for (int i = 0; i <= cnt; i++) { | for (int i = 0; i <= cnt; i++) { | ||
answer | answer[i] = s[i]; | ||
} | } | ||
for (int i = 0; i < cnt; i++) { | for (int i = 0; i < cnt; i++) { | ||
for (int j = 0; j < cnt - 1; j++) { | for (int j = 0; j < cnt - 1; j++) { | ||
if (answer | if (answer[j] <= answer[j + 1]) { | ||
SWAP(answer | SWAP(answer[j], answer[j + 1]); | ||
} | } | ||
} | } | ||
| Line 138: | Line 138: | ||
for (int i = 0; i < count; i++) { | for (int i = 0; i < count; i++) { | ||
if (isalpha(s | if (isalpha(s[i])) { | ||
answer = false; | answer = false; | ||
} | } | ||
| Line 182: | Line 182: | ||
if (cnt % 2) { | if (cnt % 2) { | ||
answer = (char*)malloc(sizeof(char)); | answer = (char*)malloc(sizeof(char)); | ||
answer | answer[0] = s[cnt / 2]; | ||
answer | answer[1] = '\0'; | ||
} | } | ||
else { | else { | ||
answer = (char*)malloc(sizeof(char) * 2); | answer = (char*)malloc(sizeof(char) * 2); | ||
answer | answer[0] = s[(cnt / 2) - 1]; | ||
answer | answer[1] = s[cnt / 2]; | ||
answer | answer[2] = '\0'; | ||
} | } | ||
return answer; | return answer; | ||
| Line 199: | Line 199: | ||
char* answer = (char*)malloc(sizeof(char) * 10); | char* answer = (char*)malloc(sizeof(char) * 10); | ||
int day = b; | int day = b; | ||
int month | int month[] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; | ||
for (int i = 0; i < a; i++) { | for (int i = 0; i < a; i++) { | ||
day += month | day += month[i - 1]; | ||
} | } | ||
switch (day % 7) { | switch (day % 7) { | ||
| Line 239: | Line 239: | ||
int* answer = (int*)malloc(sizeof(int) * 11); | int* answer = (int*)malloc(sizeof(int) * 11); | ||
do { | do { | ||
answer | answer[i++] = n % 10; | ||
n /= 10; | n /= 10; | ||
} while (n > 0); | } while (n > 0); | ||
| Line 297: | Line 297: | ||
#include <stdlib.h> | #include <stdlib.h> | ||
double solution(int arr | double solution(int arr[], size_t arr_len) { | ||
double answer = 0; | double answer = 0; | ||
int sum = 0; | int sum = 0; | ||
for(int i = 0;i<arr_len;i++) { | for(int i = 0;i<arr_len;i++) { | ||
sum += arr | sum += arr[i]; | ||
} | } | ||
answer = (double)sum/arr_len; | answer = (double)sum/arr_len; | ||
return answer; | |||
} | |||
=== 핸드폰 번호 가리기 === | |||
#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; | |||
} | |||
=== 이상한 문자 만들기 === | |||
#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; | return answer; | ||
} | } | ||
== 코테 과제 2단계 == | |||
=== 소수 만들기 === | |||
#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; | |||
} | |||
=== 피보나치 수 === | |||
#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; | |||
} | |||
=== N개의 최소공배수 === | |||
#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; | |||
} | |||
=== 최솟값 만들기 === | |||
#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; | |||
} | |||
=== 올바른 괄호 === | |||
#include <stdbool.h> | |||
#include <stdlib.h> | |||
#include <string.h> | |||
bool solution(const char* s) { | |||
bool answer = 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; | |||
} | |||
=== 주식가격 === | |||
#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; | |||
} | |||
=== 큰수만들기 === | |||
#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; | |||
} | |||
=== 스킬트리 === | |||
#include <stdbool.h> | |||
#include <stdlib.h> | |||
#include <string.h> | |||
int solution(const char* skill, const char* skill_trees[], size_t skill_trees_len) { | |||
int answer = 0; | |||
for(int i = 0;i < skill_trees_len;i++) { | |||
int index = 0; | |||
bool possible = true; | |||
for(int j = 0; j < strlen(skill_trees[i]);j++) { | |||
if(skill_trees[i][j] == skill[index]) { | |||
index++; | |||
} | |||
else { | |||
for(int x = index + 1; x < strlen(skill);x++) { | |||
if(skill_trees[i][j] == skill[x]) { | |||
possible = false; | |||
break; | |||
} | |||
} | |||
} | |||
} | |||
if(possible) { | |||
answer++; | |||
} | |||
} | |||
return answer; | |||
} | |||
=== 삼각 달팽이 === | |||
#include <stdbool.h> | |||
#include <stdlib.h> | |||
int* solution(int n) { | |||
int* answer = (int*)malloc(sizeof(int)*n*(n+1)/2); | |||
int arr[1001][1001] = {0,}; | |||
int x = 0, y = 0, dir = 0, num = 1; | |||
for (int i = 0; i < n; i++) { | |||
if(dir == 0){ | |||
for (int j = i; j < n; j++) { | |||
arr[x++][y] = num++; | |||
} | |||
x--; | |||
y++; | |||
dir = 1; | |||
} | |||
else if(dir == 1){ | |||
for (int j = i; j < n; j++) { | |||
arr[x][y++] = num++; | |||
} | |||
x--; | |||
y -= 2; | |||
dir = 2; | |||
} | |||
else if(dir == 2){ | |||
for (int j = i; j < n; j++) { | |||
arr[x--][y--] = num++; | |||
} | |||
x += 2; | |||
y++; | |||
dir = 0; | |||
} | |||
} | |||
int k = 0; | |||
for (int i = 0; i < n; i++) { | |||
for (int j = 0; j <= i; j++) { | |||
if (arr[i][j]) { | |||
answer[k++] = arr[i][j]; | |||
} | |||
} | |||
} | |||
return answer; | |||
} | |||
=== 멀쩡한 사각형 === | |||
#include <stdbool.h> | |||
#include <stdlib.h> | |||
int gcd(int a, int b) { | |||
return b ? gcd(b, a%b) : a; | |||
} | |||
long long solution(int w, int h) { | |||
long long answer = 1; | |||
long long sum = (long long)w*(long long)h; | |||
answer = sum - (w + h - gcd(w,h)); | |||
return answer; | |||
} | |||
== 묵찌빠 == | |||
#include <cmath> | |||
#include <algorithm> | |||
#include <time.h> | |||
#include <conio.h> | |||
#include <Windows.h> | |||
using namespace std; | |||
enum { | |||
ROCK = 1, | |||
SCISSOR, | |||
PAPER, | |||
WIN = 1, | |||
LOSE, | |||
DRAW | |||
}; | |||
int user_select() { | |||
int input; | |||
double start = clock() / CLOCKS_PER_SEC; | |||
while (true) { | |||
double now = clock() / CLOCKS_PER_SEC; | |||
if (_kbhit()) { | |||
input = _getch(); | |||
switch (input) { | |||
case 'r': | |||
return ROCK; | |||
break; | |||
case 's': | |||
return SCISSOR; | |||
break; | |||
case 'p': | |||
return PAPER; | |||
break; | |||
} | |||
break; | |||
} | |||
if (now - start > 5.0) { | |||
int input = rand() % 3 + 1; | |||
switch (input) { | |||
case 1: | |||
return ROCK; | |||
break; | |||
case 2: | |||
return SCISSOR; | |||
break; | |||
case 3: | |||
return PAPER; | |||
break; | |||
} | |||
break; | |||
} | |||
} | |||
} | |||
int is_winner(int user_rsp, int cpu_rsp) { | |||
switch (user_rsp) { | |||
case ROCK: | |||
if (cpu_rsp == SCISSOR) { | |||
printf("나 : 바위, 상대 : 가위\n"); | |||
return 1; | |||
} | |||
else if (cpu_rsp == PAPER) { | |||
printf("나 : 바위, 상대 : 보\n"); | |||
return 2; | |||
} | |||
else { | |||
printf("나 : 바위, 상대 : 바위\n"); | |||
return 0; | |||
} | |||
break; | |||
case SCISSOR: | |||
if (cpu_rsp == ROCK) { | |||
printf("나 : 가위, 상대 : 바위\n"); | |||
return 2; | |||
} | |||
else if (cpu_rsp == PAPER) { | |||
printf("나 : 가위, 상대 : 보\n"); | |||
return 1; | |||
} | |||
else { | |||
printf("나 : 가위, 상대 : 가위\n"); | |||
return 0; | |||
} | |||
break; | |||
case PAPER: | |||
if (cpu_rsp == ROCK) { | |||
printf("나 : 보, 상대 : 바위\n"); | |||
return 1; | |||
} | |||
else if (cpu_rsp == SCISSOR) { | |||
printf("나 : 보, 상대 : 가위\n"); | |||
return 2; | |||
} | |||
else { | |||
printf("나 : 보, 상대 : 보\n"); | |||
return 0; | |||
} | |||
break; | |||
} | |||
} | |||
int main() { | |||
int victory = 0, defeat = 0; | |||
srand(time(NULL)); | |||
int winner = 0; | |||
bool exit = false; | |||
int vic = 0; | |||
while (true) { //게임 계속하기 선택시 반복 | |||
printf("가위, 바위, 보를 선택하세요 (r : 바위, s : 가위, p : 보)\n"); | |||
while (true) { //묵찌빠 | |||
int UserRsp = user_select(); | |||
int CpuRsp = rand() % 3 + 1; | |||
winner = is_winner(UserRsp, CpuRsp); | |||
switch (UserRsp) { | |||
case ROCK: | |||
if (vic == 0) { | |||
switch (winner) { | |||
case 0: | |||
printf("비겼습니다. 다음패는 ? \n"); | |||
break; | |||
case 1: | |||
printf("이겼습니다. 다음 패는? (같은 패를 내면 이깁니다)\n"); | |||
vic = 1; | |||
break; | |||
case 2: | |||
printf("졌습니다. 다음 패는? (같은 패를 내면 집니다)\n"); | |||
vic = 2; | |||
break; | |||
} | |||
} | |||
else if (vic == 1) { | |||
switch (winner) { | |||
case 0: | |||
printf("승리했습니다\n"); | |||
victory++; | |||
exit = true; | |||
break; | |||
case 1: | |||
printf("이겼습니다. 다음 패는? (같은 패를 내면 이깁니다)\n"); | |||
break; | |||
case 2: | |||
printf("졌습니다. 다음 패는? (같은 패를 내면 집니다)\n"); | |||
vic = 2; | |||
break; | |||
} | |||
} | |||
else { | |||
switch (winner) { | |||
case 0: | |||
printf("패배했습니다\n"); | |||
defeat++; | |||
exit = true; | |||
break; | |||
case 1: | |||
printf("이겼습니다. 다음 패는? (같은 패를 내면 이깁니다)\n"); | |||
vic = 1; | |||
break; | |||
case 2: | |||
printf("졌습니다. 다음 패는? (같은 패를 내면 집니다)\n"); | |||
break; | |||
} | |||
} | |||
break; | |||
case SCISSOR: | |||
if (vic == 0) { | |||
switch (winner) { | |||
case 0: | |||
printf("비겼습니다. 다음패는 ? \n"); | |||
break; | |||
case 1: | |||
printf("이겼습니다. 다음 패는? (같은 패를 내면 이깁니다)\n"); | |||
vic = 1; | |||
break; | |||
case 2: | |||
printf("졌습니다. 다음 패는? (같은 패를 내면 집니다)\n"); | |||
vic = 2; | |||
break; | |||
} | |||
} | |||
else if (vic == 1) { | |||
switch (winner) { | |||
case 0: | |||
printf("승리했습니다\n"); | |||
victory++; | |||
exit = true; | |||
break; | |||
case 1: | |||
printf("이겼습니다. 다음 패는? (같은 패를 내면 이깁니다)\n"); | |||
break; | |||
case 2: | |||
printf("졌습니다. 다음 패는? (같은 패를 내면 집니다)\n"); | |||
vic = 2; | |||
break; | |||
} | |||
} | |||
else { | |||
switch (winner) { | |||
case 0: | |||
printf("패배했습니다\n"); | |||
defeat++; | |||
exit = true; | |||
break; | |||
case 1: | |||
printf("이겼습니다. 다음 패는? (같은 패를 내면 이깁니다)\n"); | |||
vic = 1; | |||
break; | |||
case 2: | |||
printf("졌습니다. 다음 패는? (같은 패를 내면 집니다)\n"); | |||
break; | |||
} | |||
} | |||
break; | |||
case PAPER: | |||
if (vic == 0) { | |||
switch (winner) { | |||
case 0: | |||
printf("비겼습니다. 다음패는 ? \n"); | |||
break; | |||
case 1: | |||
printf("이겼습니다. 다음 패는? (같은 패를 내면 이깁니다)\n"); | |||
vic = 1; | |||
break; | |||
case 2: | |||
printf("졌습니다. 다음 패는? (같은 패를 내면 집니다)\n"); | |||
vic = 2; | |||
break; | |||
} | |||
} | |||
else if (vic == 1) { | |||
switch (winner) { | |||
case 0: | |||
printf("승리했습니다\n"); | |||
victory++; | |||
exit = true; | |||
break; | |||
case 1: | |||
printf("이겼습니다. 다음 패는? (같은 패를 내면 이깁니다)\n"); | |||
break; | |||
case 2: | |||
printf("졌습니다. 다음 패는? (같은 패를 내면 집니다)\n"); | |||
vic = 2; | |||
break; | |||
} | |||
} | |||
else { | |||
switch (winner) { | |||
case 0: | |||
printf("패배했습니다\n"); | |||
defeat++; | |||
exit = true; | |||
break; | |||
case 1: | |||
printf("이겼습니다. 다음 패는? (같은 패를 내면 이깁니다)\n"); | |||
vic = 1; | |||
break; | |||
case 2: | |||
printf("졌습니다. 다음 패는? (같은 패를 내면 집니다)\n"); | |||
break; | |||
} | |||
} | |||
break; | |||
} | |||
if (exit) { | |||
break; | |||
} | |||
} | |||
char will; | |||
printf("게임이 끝났습니다. \n계속하기 : C | 멈추기 : Q | 기록보기 : R \n"); | |||
scanf_s("%c", &will); | |||
getchar(); | |||
if (will == 'C') { | |||
exit = false; | |||
continue; | |||
} | |||
else if (will == 'Q') { | |||
break; | |||
} | |||
else if (will == 'R') { | |||
char will2; | |||
printf("승리 : %d, 패배 : %d\n", victory, defeat); | |||
printf("계속하기 : C | 멈추기 : Q\n"); | |||
scanf_s("%c", &will2); | |||
getchar(); | |||
if (will2 == 'C') { | |||
exit = false; | |||
continue; | |||
} | |||
else if (will2 == 'Q') { | |||
break; | |||
} | |||
} | |||
} | |||
printf("\n나 %d : %d CPU\n\n", victory, defeat); | |||
printf("이용해주셔서 감사합니다. \n"); | |||
} | |||
Latest revision as of 01:08, 27 March 2026
코테 과제 1단계
약수의 합
#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;
}
하샤드 수
#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;
}
수박수박수박수?
#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;
}
두개 뽑아서 더하기
#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;
}
별찍기
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;
}
문자열 정렬
#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;
}
문자열을 정수로 바꾸기
#include <stdbool.h>
#include <stdlib.h>
int solution(const char* s) {
int answer = 0;
answer = atoi(s);
return answer;
}
문자열 다루기
#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;
}
두 정수 사이의 합
#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;
}
가운데 글자 가져오기
#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;
}
2016년
#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;
}
자릿수 더하기
#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;
}
자연수를 뒤집어 배열로 만들기
#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;
}
정수 제곱근 판별
#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;
}
짝수와 홀수
#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;
}
콜라츠 추측
#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;
}
평균 구하기
#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;
}
핸드폰 번호 가리기
#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;
}
이상한 문자 만들기
#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단계
소수 만들기
#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;
}
피보나치 수
#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;
}
N개의 최소공배수
#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;
}
최솟값 만들기
#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;
}
올바른 괄호
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
bool solution(const char* s) {
bool answer = 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;
}
주식가격
#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;
}
큰수만들기
#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;
}
스킬트리
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
int solution(const char* skill, const char* skill_trees[], size_t skill_trees_len) {
int answer = 0;
for(int i = 0;i < skill_trees_len;i++) {
int index = 0;
bool possible = true;
for(int j = 0; j < strlen(skill_trees[i]);j++) {
if(skill_trees[i][j] == skill[index]) {
index++;
}
else {
for(int x = index + 1; x < strlen(skill);x++) {
if(skill_trees[i][j] == skill[x]) {
possible = false;
break;
}
}
}
}
if(possible) {
answer++;
}
}
return answer;
}
삼각 달팽이
#include <stdbool.h>
#include <stdlib.h>
int* solution(int n) {
int* answer = (int*)malloc(sizeof(int)*n*(n+1)/2);
int arr[1001][1001] = {0,};
int x = 0, y = 0, dir = 0, num = 1;
for (int i = 0; i < n; i++) {
if(dir == 0){
for (int j = i; j < n; j++) {
arr[x++][y] = num++;
}
x--;
y++;
dir = 1;
}
else if(dir == 1){
for (int j = i; j < n; j++) {
arr[x][y++] = num++;
}
x--;
y -= 2;
dir = 2;
}
else if(dir == 2){
for (int j = i; j < n; j++) {
arr[x--][y--] = num++;
}
x += 2;
y++;
dir = 0;
}
}
int k = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j <= i; j++) {
if (arr[i][j]) {
answer[k++] = arr[i][j];
}
}
}
return answer;
}
멀쩡한 사각형
#include <stdbool.h>
#include <stdlib.h>
int gcd(int a, int b) {
return b ? gcd(b, a%b) : a;
}
long long solution(int w, int h) {
long long answer = 1;
long long sum = (long long)w*(long long)h;
answer = sum - (w + h - gcd(w,h));
return answer;
}
묵찌빠
#include <cmath>
#include <algorithm>
#include <time.h>
#include <conio.h>
#include <Windows.h>
using namespace std;
enum {
ROCK = 1,
SCISSOR,
PAPER,
WIN = 1,
LOSE,
DRAW
};
int user_select() {
int input;
double start = clock() / CLOCKS_PER_SEC;
while (true) {
double now = clock() / CLOCKS_PER_SEC;
if (_kbhit()) {
input = _getch();
switch (input) {
case 'r':
return ROCK;
break;
case 's':
return SCISSOR;
break;
case 'p':
return PAPER;
break;
}
break;
}
if (now - start > 5.0) {
int input = rand() % 3 + 1;
switch (input) {
case 1:
return ROCK;
break;
case 2:
return SCISSOR;
break;
case 3:
return PAPER;
break;
}
break;
}
}
}
int is_winner(int user_rsp, int cpu_rsp) {
switch (user_rsp) {
case ROCK:
if (cpu_rsp == SCISSOR) {
printf("나 : 바위, 상대 : 가위\n");
return 1;
}
else if (cpu_rsp == PAPER) {
printf("나 : 바위, 상대 : 보\n");
return 2;
}
else {
printf("나 : 바위, 상대 : 바위\n");
return 0;
}
break;
case SCISSOR:
if (cpu_rsp == ROCK) {
printf("나 : 가위, 상대 : 바위\n");
return 2;
}
else if (cpu_rsp == PAPER) {
printf("나 : 가위, 상대 : 보\n");
return 1;
}
else {
printf("나 : 가위, 상대 : 가위\n");
return 0;
}
break;
case PAPER:
if (cpu_rsp == ROCK) {
printf("나 : 보, 상대 : 바위\n");
return 1;
}
else if (cpu_rsp == SCISSOR) {
printf("나 : 보, 상대 : 가위\n");
return 2;
}
else {
printf("나 : 보, 상대 : 보\n");
return 0;
}
break;
}
}
int main() {
int victory = 0, defeat = 0;
srand(time(NULL));
int winner = 0;
bool exit = false;
int vic = 0;
while (true) { //게임 계속하기 선택시 반복
printf("가위, 바위, 보를 선택하세요 (r : 바위, s : 가위, p : 보)\n");
while (true) { //묵찌빠
int UserRsp = user_select();
int CpuRsp = rand() % 3 + 1;
winner = is_winner(UserRsp, CpuRsp);
switch (UserRsp) {
case ROCK:
if (vic == 0) {
switch (winner) {
case 0:
printf("비겼습니다. 다음패는 ? \n");
break;
case 1:
printf("이겼습니다. 다음 패는? (같은 패를 내면 이깁니다)\n");
vic = 1;
break;
case 2:
printf("졌습니다. 다음 패는? (같은 패를 내면 집니다)\n");
vic = 2;
break;
}
}
else if (vic == 1) {
switch (winner) {
case 0:
printf("승리했습니다\n");
victory++;
exit = true;
break;
case 1:
printf("이겼습니다. 다음 패는? (같은 패를 내면 이깁니다)\n");
break;
case 2:
printf("졌습니다. 다음 패는? (같은 패를 내면 집니다)\n");
vic = 2;
break;
}
}
else {
switch (winner) {
case 0:
printf("패배했습니다\n");
defeat++;
exit = true;
break;
case 1:
printf("이겼습니다. 다음 패는? (같은 패를 내면 이깁니다)\n");
vic = 1;
break;
case 2:
printf("졌습니다. 다음 패는? (같은 패를 내면 집니다)\n");
break;
}
}
break;
case SCISSOR:
if (vic == 0) {
switch (winner) {
case 0:
printf("비겼습니다. 다음패는 ? \n");
break;
case 1:
printf("이겼습니다. 다음 패는? (같은 패를 내면 이깁니다)\n");
vic = 1;
break;
case 2:
printf("졌습니다. 다음 패는? (같은 패를 내면 집니다)\n");
vic = 2;
break;
}
}
else if (vic == 1) {
switch (winner) {
case 0:
printf("승리했습니다\n");
victory++;
exit = true;
break;
case 1:
printf("이겼습니다. 다음 패는? (같은 패를 내면 이깁니다)\n");
break;
case 2:
printf("졌습니다. 다음 패는? (같은 패를 내면 집니다)\n");
vic = 2;
break;
}
}
else {
switch (winner) {
case 0:
printf("패배했습니다\n");
defeat++;
exit = true;
break;
case 1:
printf("이겼습니다. 다음 패는? (같은 패를 내면 이깁니다)\n");
vic = 1;
break;
case 2:
printf("졌습니다. 다음 패는? (같은 패를 내면 집니다)\n");
break;
}
}
break;
case PAPER:
if (vic == 0) {
switch (winner) {
case 0:
printf("비겼습니다. 다음패는 ? \n");
break;
case 1:
printf("이겼습니다. 다음 패는? (같은 패를 내면 이깁니다)\n");
vic = 1;
break;
case 2:
printf("졌습니다. 다음 패는? (같은 패를 내면 집니다)\n");
vic = 2;
break;
}
}
else if (vic == 1) {
switch (winner) {
case 0:
printf("승리했습니다\n");
victory++;
exit = true;
break;
case 1:
printf("이겼습니다. 다음 패는? (같은 패를 내면 이깁니다)\n");
break;
case 2:
printf("졌습니다. 다음 패는? (같은 패를 내면 집니다)\n");
vic = 2;
break;
}
}
else {
switch (winner) {
case 0:
printf("패배했습니다\n");
defeat++;
exit = true;
break;
case 1:
printf("이겼습니다. 다음 패는? (같은 패를 내면 이깁니다)\n");
vic = 1;
break;
case 2:
printf("졌습니다. 다음 패는? (같은 패를 내면 집니다)\n");
break;
}
}
break;
}
if (exit) {
break;
}
}
char will;
printf("게임이 끝났습니다. \n계속하기 : C | 멈추기 : Q | 기록보기 : R \n");
scanf_s("%c", &will);
getchar();
if (will == 'C') {
exit = false;
continue;
}
else if (will == 'Q') {
break;
}
else if (will == 'R') {
char will2;
printf("승리 : %d, 패배 : %d\n", victory, defeat);
printf("계속하기 : C | 멈추기 : Q\n");
scanf_s("%c", &will2);
getchar();
if (will2 == 'C') {
exit = false;
continue;
}
else if (will2 == 'Q') {
break;
}
}
}
printf("\n나 %d : %d CPU\n\n", victory, defeat);
printf("이용해주셔서 감사합니다. \n");
}