10

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 4

26(도전문제)

#include <stdio.h>
#define SIZE 10

int main(void)
{
char ans1;
int ans2, ans3, i;
int seats[SIZE] = { 0 };
while (1)
{
printf("좌석을 예약하시겠습니까?(y 또는 n) ");
scanf_s(" %c", &ans1);
if (ans1 == 'y')
{
printf("-------------------------------\n");
printf(" 1 2 3 4 5 6 7 8 9 10\n");
printf("-------------------------------\n");
for (i = 0; i < SIZE; i++)
printf(" %d", seats[i]);
printf("\n");
printf("몇번째 좌석을 예약하시겠습니까 ? ");
printf("두개의 좌석 입력해주세요. 예를 들어서...... 1, 3\n");

scanf_s("%d, %d", &ans2, &ans3);

if (ans2 <= 0 || ans2 > SIZE || ans3 <= 0 || ans3 > SIZE) {
printf("1 부터 10 사이의 숫자 두 개를 입력하세요\n");
continue;
}

if (ans2 == ans3) {
printf("서로 다른 숫자를 입력 하세요\n");
continue;
}

if (seats[ans2 - 1] == 0 && seats[ans3 - 1] == 0) { // 예약되지


않았으면
seats[ans2 - 1] = 1;
seats[ans3 - 1] = 1;
printf("예약되었습니다.\n");
}
else // 이미 예약되었으면
printf("이미 예약된 자리입니다.\n");
}
else if (ans1 == 'n')
return 0;
}
return 0;
}
31(도전문제)

#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 10

int main(void)
{
int prices[SIZE] = { 0 };
int i, maximum;
printf("---------------------------------------\n");
printf("1 2 3 4 5 6 7 8 9 10\n");
printf("---------------------------------------\n");
srand((unsigned)time(NULL));
for (i = 0; i < SIZE; i++) {
prices[i] = (rand() % 100) + 1;
printf("%-3d ", prices[i]);
}
printf("\n\n");
maximum = prices[0];
for (i = 1; i < SIZE; i++)
{
if (prices[i] > maximum)
maximum = prices[i];
}
printf("최댓값은 %d 입니다.\n", maximum);
return 0;
}

52(도전)

#include <stdio.h>
#include <stdlib.h>

void init_board(char board[3][3]);


void disp_board(char board[3][3]);
int CheckIsGameEnd(char board[3][3]);

int main(void)
{
char board[3][3], player;
int k, x, y;
init_board(board);
int temp;
bool isComputerTurnEnd = false;

for (k = 0; k < 9; k++) {


player = (k % 2 == 0) ? 'X' : 'O';

if (player == 'O') {

while (1) {
printf("player % c -> (x, y) 좌표 : ", player);
scanf_s("%d %d", &x, &y);
if (board[x][y] == ' ') break; // 비어있는 좌표 입력
else printf("에러 : 이미 입력된 좌표 !!\n");
}
board[x][y] = player;
disp_board(board);
}
else {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board[i][j] == ' ') {

board[i][j] = player;
disp_board(board);

isComputerTurnEnd = true;
break;
}
}
if (isComputerTurnEnd)break;
}
isComputerTurnEnd = false;
}

temp = CheckIsGameEnd(board);
if (temp != -1) {
printf("게임이 끝났습니다!");
return 0;
}
}

return 0;
}

void init_board(char board[3][3])


{
int x, y;
for (x = 0; x < 3; x++)
for (y = 0; y < 3; y++) board[x][y] = ' '; // space character
}
void disp_board(char board[3][3])
{
int i;
for (i = 0; i < 3; i++) {
printf("---|---|---\n");
printf(" %c | %c | %c \n", board[i][0], board[i][1], board[i][2]);
}
printf("---|---|---\n");
}

int CheckIsGameEnd(char board[3][3]) {

for (int i = 0;i < 3;i++) {


if (board[i][0] != ' ' && board[i][0] == board[i][1] && board[i][1] ==
board[i][2])
return (board[i][0] == 'X') ? 0 : 1;
}

for (int i = 0;i < 3;i++) {


if (board[0][i] != ' ' && board[0][i] == board[1][i] && board[1][i] ==
board[2][i])
return (board[0][i] == 'X') ? 0 : 1;
}

if (board[0][0] != ' ' && board[0][0] == board[1][1] && board[1][1] ==


board[2][2])
return (board[0][0] == 'X') ? 0 : 1;

if (board[0][2] != ' ' && board[0][2] == board[1][1] && board[1][1] ==


board[2][0])
return (board[0][2] == 'X') ? 0 : 1;

return -1;
}

You might also like