1. 묵찌빠 게임 ¶
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <conio.h>
#include <windows.h>
enum{
rock = 1,
scissor = 2,
paper = 3,
win = 1,
lose = 2,
draw = 3,
};
int determine_rsp();
int determine_winner(int player, int CPU);
void announce_winner(int player, int CPU, int Result);
int main() {
srand((unsigned int)time(NULL));
while (1) {
int player_point = 0;
int CPU_point = 0;
//게임 진행
while (player_point != 2 && CPU_point != 2) {
system("cls");
//전판 이긴사람
printf("공격권 : ");
if (player_point == 1) {
printf("당신\n");
}
else if (CPU_point == 1) {
printf("상대\n");
}
else {
printf("없음\n");
}
//가위바위보
int cpu_rsp = rand() % 3 + 1;
int player_rsp = 0;
int result = 0;
printf("가위, 바위, 보 세가지 중 무엇을 낼까?\n");
printf("(가위: s, 바위: r, 보: p)\n");
player_rsp = determine_rsp();
result = determine_winner(player_rsp, cpu_rsp);
announce_winner(player_rsp, cpu_rsp, result);
//공격권
switch (result) {
case win:
player_point++;
CPU_point = 0;
break;
case lose:
player_point = 0;
CPU_point++;
break;
}
}
//게임 최종 결과
system("cls");
printf("최종 승자! : ");
if (player_point == 2) {
printf("당신\n");
}
if (CPU_point == 2) {
printf("상대\n");
}
Sleep(3000);
// 게임 진행 여부 묻기
// 계속하기 : C 멈추기 : Q 기록보기 : R
printf("계속 하시겠습니까?\n");
printf("계속하기 : c 멈추기 : q 기록보기 : r\n");
while (1) {
char choice = _getch();
switch (choice) {
case 'c':
printf("게임을 계속 진행합니다.\n");
Sleep(1000);
break;
case 'q':
printf("잠시후 게임을 종료합니다.\n");
Sleep(1000);
exit(0);
break;
case 'r':
printf("미구현!\n");
Sleep(1000);
printf("계속 하시겠습니까?\n");
printf("계속하기 : c 멈추기 : q 기록보기 : r\n");
break;
default:
printf("{c, q, r}중에서 입력하세요!\n");
break;
}
if (choice == 'c') {
break;
}
}
}
}
// 게임 끝났다고 출력
// scanf 써서
// 계속하겠습니까? 멈추겠습니까?
// 계속하기 : C 멈추기 : Q 기록보기 : R
// R을 누르면 기록을 보여주기. (CPU 몇승 플레이어 몇승)
// C를 누르면 다시 한판 하기
int determine_rsp() {
//기준 시간 선언
int past_time = clock() / CLOCKS_PER_SEC;
// 현재 시간 선언
int current_time;
while (1) {
current_time = clock() / CLOCKS_PER_SEC;
// 사용자의 키보드 입력 여부를 검사
if (_kbhit()) {
char select = _getch();
switch (select) {
case 'r':
return rock;
case 's':
return scissor;
case 'p':
return paper;
default:
printf("{r, s, p}중에서 입력하세요!\n");
}
}
// 현재 시간이 기준시간으로부터 5초가 지났을 때 시행
if (current_time - past_time > 5) {
return rand() % 3 + 1;
}
Sleep(20);
}
}
int determine_winner(int player, int CPU) {
if (CPU - player == 1 || CPU - player == -2) {
return win;
}else if(CPU - player == 0){
return draw;
}else {
return lose;
}
}
void announce_winner(int player, int CPU, int Result) {
system("cls");
printf("안내면 진거!\n");
Sleep(1000);
printf("가위!\n");
Sleep(500);
printf("바위!\n");
Sleep(500);
printf("보!\n");
Sleep(500);
printf("당신 : ");
switch (player) {
case rock:
printf("묵");
break;
case scissor:
printf("찌");
break;
case paper:
printf("빠");
break;
}
printf(" ");
printf("상대 : ");
switch (CPU) {
case rock:
printf("묵");
break;
case scissor:
printf("찌");
break;
case paper:
printf("빠");
break;
}
printf("\n");
switch(Result){
case win:
printf("이겼습니다\n");
break;
case lose:
printf("졌습니다\n");
break;
case draw:
printf("비겼습니다\n");
break;
}
printf("계속하려면 아무키나 누르시오\n");
while (1) {
if (_kbhit()) {
Sleep(100);
break;
}
}
}
2.1. 두 개 뽑아서 더하기 ¶
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
int compare(void *a,void *b);
int* solution(int numbers[], size_t numbers_len) {
qsort(numbers,numbers_len,sizeof(int),compare);
int* temp = (int*)malloc(sizeof(int)*201);
memset(temp,0,sizeof(int));
int i,j,k,n=0;
for (i = 0; i < numbers_len - 1; ++i){
for(j = i+1; j < numbers_len; ++j){
int count = 0;
for(k=0; k < n; ++k){
if(temp[k] == numbers [i] + numbers[j]){
++count;
}
}
if(count == 0){
temp[n] = numbers [i] + numbers[j];
++n;
}
}
}
qsort(temp,n,sizeof(int),compare);
int* answer = (int*)malloc(sizeof(int)*n);
for (i = 0; i < n; ++i){
answer[i]= temp[i];
}
free(temp);
return answer;
}
int compare(void *a,void *b){
int num1 = *(int *)a;
int num2 = *(int *)b;
return num1 - num2;
}
2.2. 2016년 ¶
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
char* solution(int a, int b) {
int days[12] = {31,29,31,30,31,30,31,31,30,31,30,31};
char week[7][4] = {"FRI","SAT","SUN","MON","TUE","WED","THU"};
char* answer = (char*)malloc(sizeof(char)*4);
int i, d, n=-1;
for (i = 0; i < a - 1; ++i) {
n += days[i];
}
n += b;
d = n % 7;
strcpy(answer, week[d]);
return answer;
}
2.3. 가운데 글자 가져오기 ¶
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
char* solution(const char* s) {
int len = strlen(s);
if (len % 2) {
char* answer = (char*)malloc(sizeof(char) * 2);
answer[0] = s[len / 2];
answer[1] = '\0';
printf("%s", answer);
return answer;
}else{
char* answer = (char*)malloc(sizeof(char) * 3);
answer[0] = s[len / 2 - 1];
answer[1] = s[len / 2];
answer[2] = '\0';
printf("%s", answer);
return answer;
}
}
2.4. 두 정수 사이의 합 ¶
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
long long solution(int a, int b) {
long long answer = 0;
int i;
if (a>b){
int temp;
temp = b;
b = a;
a = temp;
}
for (i = a; i <= b ; ++i){
answer += i;
}
return answer;
}
2.5. 문자열 내림차순으로 배치하기 ¶
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
char* solution(const char* s) {
int i,j, len =strlen(s);
char* answer = (char*)malloc(sizeof(char)*len + 1);
strcpy(answer,s);
for(i = len; i > 0; --i){
for(j = 0; j < i - 1; ++j){
if (answer[j] < answer[j+1]){
char temp;
temp = answer[j+1];
answer[j+1] = answer[j];
answer[j] = temp;
}
}
}
return answer;
}
2.6. 문자열 다루기 기본 ¶
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
bool solution(const char* s) {
bool t1, t2, answer;
int i, count, len = strlen(s);
if(len == 4 || len == 6){
t1 = true;
}else{
t1 = false;
}
count = 0;
for(i = 0 ;i < len; ++i){
if(isdigit(s[i])){
++count;
}
}
if(count == len){
t2 = true;
}else{
t2 = false;
}
if(t1&&t2){
answer = true;
}else{
answer = false;
}
return answer;
}
2.7. 수박수박수박수박수박수? ¶
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
char* solution(int n) {
// 리턴할 값은 메모리를 동적 할당해주세요.
char* answer = (char*)malloc(sizeof("수") * n + 1);
int i;
memset(answer,'\0',sizeof("수") * n + 1);
for(i = 0;i < n; ++i){
if(i%2==0){
strcat(answer,"수");
}else{
strcat(answer,"박");
}
answer[n*sizeof("수")] = '\0';
}
return answer;
}
2.8. 문자열을 정수로 바꾸기 ¶
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
int solution(const char* s) {
int answer = atoi(s);
return answer;
}
2.9. 약수의 합 ¶
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
int solution(int n) {
int i, answer = 0;
for(i = 1; i <= n; ++i){
if(n%i == 0){
answer += i;
}
}
return answer;
}
2.10. 이상한 문자 만들기 ¶
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
char* solution(const char* s){
const int len = strlen(s);
char* answer = (char*)malloc(sizeof(char) * strlen(s) + 1);
int i = 0, count = 0;
memset(answer, '\0', sizeof(char) * strlen(s) + 1);
while (count < len) {
if (s[count] != ' ') {
if (i % 2 == 0) {
answer[count] = (char)toupper(s[count]);
}
else {
answer[count] = (char)tolower(s[count]);
}
i++;
}
else {
answer[count] = s[count];
i = 0;
}
count++;
}
answer[strlen(s)] = '\0';
return answer;
}
2.11. 자릿수 더하기 ¶
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <math.h>
int solution(int n) {
int i = 0, answer = 0;
while (n % (int)pow(10, i) != n) {
answer += (n % (int)pow(10, i + 1))/(int)pow(10, i);
i++;
}
return answer;
}
2.12. 자연수 뒤집어 배열로 만들기 ¶
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
int* solution(long long n) {
int i = 0;
long long m = n;
while (m != 0) {
m = m / 10;
i++;
}
int* answer = (int*)malloc(sizeof(int) * i);
for (int j = 0; n != 0; ++j) {
answer[j] = n % 10;
n = n / 10;
}
return answer;
}
2.13. 정수 제곱근 판별 ¶
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <math.h>
long long solution(long long n) {
if (sqrt(n) == (int)sqrt(n)){
return pow(sqrt(n) + 1,2);
}else{
return -1;
}
}
2.14. 짝수와 홀수 ¶
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
char* solution(int num) {
char* answer = (char*)malloc(sizeof(char)*4 + 1);
if (num % 2 == 0){
strcpy(answer,"Even");
}else{
strcpy(answer,"Odd");
}
return answer;
}
2.15. 콜라츠 추측 ¶
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
int solution(int num) {
long lnum = num;
int answer = 0;
while(lnum != 1){
if (answer>500){
return -1;
}
if(lnum % 2 == 0){
lnum /= 2;
}else{
lnum = lnum*3 + 1;
}
answer++;
}
return answer;
}
2.16. 평균 구하기 ¶
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
// arr_len은 배열 arr의 길이입니다.
double solution(int arr[], size_t arr_len) {
double answer = 0;
int i;
for(i = 0; i < arr_len; ++i){
answer += arr[i];
}
answer /= arr_len;
return answer;
}
2.17. 하샤드 수 ¶
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
bool solution(int x) {
int sum = 0,n = x;
while (n != 0){
sum += n % 10;
n = n / 10;
}
if(x % sum == 0){
return true;
}else{
return false;
}
}
2.18. 핸드폰 번호 가리기 ¶
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
// 파라미터로 주어지는 문자열은 const로 주어집니다. 변경하려면 문자열을 복사해서 사용하세요.
char* solution(const char* phone_number) {
int len = strlen(phone_number);
char* answer = (char*)malloc(sizeof(char)*len + 1);
int i;
strcpy(answer,phone_number);
for (i = 0; i < len - 4 ; ++i){
answer[i] = '*';
}
return answer;
}










