๐—ฃ๐—ฟ๐—ผ๐—ท๐—ฒ๐—ฐ๐˜ ๐—”๐—ฉ๐—ฆ

Project AVS Since 2023.01.27

๐Ÿ’ป ์†Œํ”„ํŠธ์›จ์–ด | Software/ํ”„๋กœ๊ทธ๋ž˜๋ฐ | C์–ธ์–ด | C

์œค์„ฑ์šฐ ์—ดํ˜ˆ C ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋„์ „! ํ”„๋กœ๊ทธ๋ž˜๋ฐ 3 ๋‹ต

์•„๋ฒต AVS 2023. 5. 22. 15:28

์œค์„ฑ์šฐ ์—ดํ˜ˆ 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 ๋‹ต

 

์œค์„ฑ์šฐ ์—ดํ˜ˆ C ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋„์ „! ํ”„๋กœ๊ทธ๋ž˜๋ฐ 1 ๋‹ต

์œค์„ฑ์šฐ ์—ดํ˜ˆ C ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋„์„œ๋‚ด์— ์žˆ๋Š” ๋„์ „ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋‹ต ํ•„์ž๊ฐ€ ๋…ํ•™ํ•˜๋ฉด์„œ ์ง์ ‘ ์“ด ์ฝ”๋“œ์ด๋ฉฐ ์ปดํŒŒ์ผ ํ›„ ์—ฌ๋Ÿฌ ๊ฐ’ ๋Œ€์ž… ํ›„ ์˜ค๋ฅ˜ ์—†์œผ๋ฉด ์—…๋ฐ์ดํŠธ โ— ๋„์ „ 1 - 16์ง„์ˆ˜ ์ถœ๋ ฅ #define _CRT_SECURE_NO_WARN

avs-won.tistory.com

2023.05.03 - [์†Œํ”„ํŠธ์›จ์–ด | Software/ํ”„๋กœ๊ทธ๋ž˜๋ฐ | C์–ธ์–ด | C] - ์œค์„ฑ์šฐ ์—ดํ˜ˆ C ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋„์ „! ํ”„๋กœ๊ทธ๋ž˜๋ฐ 2 ๋‹ต

 

์œค์„ฑ์šฐ ์—ดํ˜ˆ C ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋„์ „! ํ”„๋กœ๊ทธ๋ž˜๋ฐ 2 ๋‹ต

์œค์„ฑ์šฐ ์—ดํ˜ˆ C ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋„์„œ๋‚ด์— ์žˆ๋Š” ๋„์ „ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋‹ต ํ•„์ž๊ฐ€ ๋…ํ•™ํ•˜๋ฉด์„œ ์ง์ ‘ ์“ด ์ฝ”๋“œ์ด๋ฉฐ ์ปดํŒŒ์ผ ํ›„ ์—ฌ๋Ÿฌ ๊ฐ’ ๋Œ€์ž… ํ›„ ์˜ค๋ฅ˜ ์—†์œผ๋ฉด ์—…๋ฐ์ดํŠธ ์ด์ „ ๊ธ€ ํ”„๋กœ๊ทธ๋ž˜๋ฐ 1ํŽธ 2023.04.20 - [์†Œํ”„ํŠธ์›จ

avs-won.tistory.com

 

๋ฐ˜์‘ํ˜•