윤성우 열혈 C 프로그래밍 도서내에 있는 도전 프로그래밍 답
필자가 독학하면서 직접 쓴 코드이며
컴파일 후 여러 값 대입 후 오류 없으면
업데이트
● 도전 1 - 2차원 배열 회전
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
void showarray(int arr[][4])
{
int i,j;
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
printf("%d ", arr[i][j]);
}
printf("\n");
}
}
void Rotatearray(int(*arr)[4])
{
int i, j;
int temp[4][4];
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
temp[i][j] = arr[3-j][i];//90도 돌렸을때 각 자리의 좌표값을 원본과 비교 후 각 줄의 특징을 살핌
}
}
for (int i = 0; i < 4; i++)//temp(결과값)을 arr로 복사
for (int j = 0; j < 4; j++)
arr[i][j] = temp[i][j];
}
int main(void)
{
int array[4][4] = {
{1,2,3,4},
{5,6,7,8},
{9,10,11,12},
{13,14,15,16}
};
for (int i = 0; i < 4; i++)
{
showarray(array);//배열의 요소들을 출력
printf("\n");
Rotatearray(array);//배열의 요소들을 90도 회전
}
return 0;
}
● 도전 2 - 달팽이 배열 회전 알고리즘
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main(void)
{
int array[50][50];
int cnt = 1;
int input;
printf("원하는 n x n 배열을 출력 해드립니다. : ");
scanf("%d", &input);
for (int i = 0; i < input; i++)
{
for (int j = 0; j < input; j++)
{
array[i][j] = 0;
}
printf("\n");
}
int i = 0, j = 0;
while (cnt<=input*input)
{
while (array[i][j] == 0) //오른쪽 채우기
{
array[i][j] = cnt;
j++, cnt++;
}
i++,j--;
while (array[i][j]==0) //아래로 채우기
{
array[i][j] = cnt;
cnt++, i++;
}
i--, j--;
while (array[i][j] == 0) //왼쪽으로 채우기
{
array[i][j] = cnt;
cnt++, j--;
}
i--, j++;
while (array[i][j] == 0) //위로 채우기
{
array[i][j] = cnt;
cnt++, i--;
}
i++, j++;
}
for (int i = 0; i < input; i++) //출력
{
for (int j = 0; j < input; j++)
{
printf("%d ", array[i][j]);
}
printf("\n");
}
return 0;
}
어려운 문제중 하나였다.
필자는 N x N 배열에서 달팽이 배열을 풀었다.
N x M 으로 올라갈 경우 난이도가 조금 더 상승할 것으로 보인다.
일단 배열을 만들고 초기화가 안되어 있으니 사용자가 입력한 만큼의 공간을
0으로 초기화 했다. 그러면서 while문을 통해 0이 아닌 지점을 만나면 종료를 반복
차후 코드를 조금 더 다듬을 수 있겠지만
오늘은 이 문제를 해결했다는 사실에 집중 하고 싶다.
● 도전 3 - 난수 생성 99까지
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i;
printf("난수의 범위 0부터 99까지 \n");
for ( i = 0; i < 5; i++)
printf("난수 출력 : %d \n", rand()%100);
return 0;
}
● 도전 4 - 2개의 주사위 결과 출력
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int ReturnDice(void)
{
return rand() % 6 + 1;
}
int main(void)
{
int i;
srand((int)time(NULL));
for (i = 0; i < 2; i++)
{
printf("주사위 %d의 결과 %d \n", i + 1, ReturnDice());
}
return 0;
}
도전 3의 문제에는 1가지 문제가 있다
바로 랜덤 함수를 썻지만 처음에 나온 값이 계속해서 반복된다.
따라서 우리는 컴퓨터 시간에 따라 달라지는 난수를 생성한다.
srand함수 : seed값을 전달 받아 난수를 생성
time 함수 : 프로그램 실행시 마다 다른 값을 얻기 위해 선언
● 도전 5 - 컴퓨터랑 가위 바위 보 게임
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int USER_RPS(void);
int COM_RPS(void);
int Win(int i1, int i2);
int main(void)
{
int win = 0, same = 0, computer, user, i,result;
srand((int)time(NULL));
while (1)
{
user = USER_RPS();
computer = COM_RPS();
result = Win(user, computer);
if (result == 1)
{
printf("당신이 이겼습니다.\n");
win++;
}
else if (result == 0)
{
printf("당신은 비겼습니다.\n");
same++;
}
else {
printf("당신은 졌습니다.\n");
break;
}
}
printf("게임의 결과 : %d승, %d무 \n", win, same);
return 0;
}
int USER_RPS(void)
{
int input;
printf("바위는 1 ,가위는 2 , 보는 3 :");
scanf("%d", &input);
if (input == 1)
printf("당신은 바위 선택, ");
else if (input == 2)
printf("당신은 가위 선택, ");
else
printf("당신은 보 선택, ");
return input;
}
int COM_RPS(void)
{
int computer = rand() % 3 + 1;
if (computer == 1)
printf("컴퓨터는 바위 선택, ");
else if (computer == 2)
printf("컴퓨터는 가위 선택, ");
else
printf("컴퓨터는 보 선택, ");
return computer;
}
int Win(int i1, int i2)
{
if (i1 == i2)
return 0;
else if (i1%3 == (i2+1)%3)
return 2;
else
return 1;
}
우승 비교할때 1,2,3으로 비교 보다
0,1,2 로 비교
● 도전 6 - 숫자 야구 프로그램
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void Com_choice(int com[]);
int User_choice(int user[]);
int check(int com[], int user[]);
int cnt = 0;
int main(void)
{
int com_input[3];
int user_input[3];
int count = 1;
int result=0;
printf("Start Game\n");
Com_choice(com_input);
while (result != 1) //사용자가 맞출 때 까지 진행
{
User_choice(user_input);
result=check(com_input, user_input);
}
printf("Game End\n");
return 0;
}
void Com_choice(int com[]) //컴퓨터의 선택
{
srand((int)(time(NULL)));
com[0] = rand() % 10;
do {
com[1] = rand() % 10;
} while (com[0] == com[1]);
do {
com[2] = rand() % 10;
} while (com[0] == com[2] || com[1]==com[2]);
}
int User_choice(int user[])
{
printf("3개의 숫자 선택:");
scanf("%d %d %d", &user[0], &user[1], &user[2]);
}
int check(int com[], int user[])
{
int strike = 0, ball = 0;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (com[i] == user[j]) {
i == j ? strike++: ball++;
}
}
}
cnt++;
printf("%d번째 도전 결과 : %d스트라이크 , %d 볼 \n", cnt, strike, ball);
if (strike == 3)
return 1;
else
return -1;
}
성공
이전 글 프로그래밍
2023.04.20 - [소프트웨어 | Software/프로그래밍 | C언어 | C] - 윤성우 열혈 C 프로그래밍 도전! 프로그래밍 1 답
2023.05.03 - [소프트웨어 | Software/프로그래밍 | C언어 | C] - 윤성우 열혈 C 프로그래밍 도전! 프로그래밍 2 답
반응형
'소프트웨어 | Software > 프로그래밍 | C언어 | C' 카테고리의 다른 글
내가 보려고 만드는 C언어 정보 모음 #구조체 #포인터 (0) | 2023.06.08 |
---|---|
내가 보려고 만드는 C언어 정보 모음 #파일 입출력 #스트림 #버퍼 #문자열 함수 (0) | 2023.06.01 |
내가 보려고 만드는 C언어 정보 모음(심화) #배열 #포인터 (0) | 2023.05.10 |
윤성우 열혈 C 프로그래밍 도전! 프로그래밍 2 답 (0) | 2023.05.03 |
내가 보려고 만드는 C언어 정보 모음 #배열 #포인터 (0) | 2023.04.25 |