Programming/C

윤성우의 열혈c프로그래밍 Chapter 20 도전3 ,도전4,도전6

fishersheep 2021. 8. 14. 02:39
반응형
#include <stdio.h>
#include <stdlib.h>
int main()
{
	int i;
	printf("난수의 범위:0부터99까지 \n");

	for (i = 0; i < 5; i++)
	{
		printf("난수출력:%d \n", rand() % 100);
	}


	return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
	srand((int)time(NULL));

	printf("주사위1의결과 %d \n주사위2의 결과 %d \n", rand() % 7, rand() % 7);

	return 0;
}

도전6 야구게임(숫자맞추기)

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>


int main()
{
	int user[3] = { 0, };
	int computer[3] = { 0, };
	int count=2;
	int strike = 0;
	int ball = 0;
	srand(time(NULL));

	for (int i = 0; i < 3; i++)
	{	
		while (1)
		{	
			computer[i] = rand() % 10;
			int check = 0;
			for (int j = 0; j < i; j++)
			{
				if (computer[j] == computer[i])
				{
					check = 1;
					break;
				}
			}
			if (!check)
				break;
		}
	}

	// printf("%d %d %d \n", computer[0], computer[1], computer[2]);

	while (strike < 3)
	{	
		printf("\n");
		printf("Start Game \n");
		printf("3개의 숫자선택:");
		for (int i = 0; i < 3; i++)
		{
			scanf("%d", &user[i]);
		}

			if (user[0] == computer[0])
				strike++;
			if (user[1] == computer[1])
				strike++;
			if (user[2] == computer[2])
				strike++;
			if (user[0] == computer[2] || user[0] == computer[1])
				ball++;
			if (user[1] == computer[2] || user[1] == computer[0])
				ball++;
			if (user[2] == computer[1] || user[2] == computer[0])
				ball++;

			if (strike == 3)
			{
				printf("%d번째 도전결과: %dstrike, %dball \n", count, strike, ball);
				break;
			}
			else
				printf("%d번째 도전결과: %dstrike, %dball \n", count, strike, ball);
		strike = 0;
		ball = 0;

		count++;
	}
	
	printf("Game Over");

	return 0;
}
반응형

'Programming > C' 카테고리의 다른 글

c언어 배열90도씩이동 , 달팽이 배열  (0) 2021.08.14
백준 2501 (C언어)  (0) 2021.08.14
c언어 소수구하기 예제  (0) 2021.08.14
c언어 학번으로다이아만들기  (0) 2021.08.13
c언어 모음개수구하기  (0) 2021.08.13