Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
Tags
- 사물인터넷
- Reflex
- 코테
- 풀스택프레임워크
- 빅데이터
- java정리
- 코딩테스트
- 백준
- Python
- 자바정리
- frontend
- 모의 주식
- 개인프로젝트
- java
- C++
- 코딩독학
- backend
- 코스닥
- 프로그램
- SNS
- 프로그래밍
- 주가예측
- 코딩
- 자바독학
- 프로그래머스
- 개발
- 이웃한 칸
- 자바
- Project
- 개발자
Archives
- Today
- Total
Creative Code
baseball.c(숫자 야구게임) 본문
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
srand(time(NULL));
int question[3];
while(1){ // 정답이 unique하게 3개 생성될 때 까지 생성
for (int i = 0; i<3; i++){
question[i] = rand()%10 +1;
}
if (question[0] != question[1] && question[1] != question[2] && question[0] != question[2]){
break;
}
}
int count = 1; // 시도 횟수
while (1) {
int strike = 0; // strike의 갯수
int ball = 0; // ball 의 갯수
int answer[3];
printf("정답 입력: "); // 사용자에게 숫자 입력 받기
for (int i = 0; i<3; i++){
scanf("%d",&answer[i]);
}
for (int i = 0; i<3; i++){ // 사용자로부터 입력받은 숫자중 숫자가 포함되는 것의 갯수
for (int j = 0; j<3; j++){
if (answer[i] == question[j]){
ball++;
}
}
}
for (int i = 0; i<3; i++){ // 숫자와 위치가 정확하게 일치하는 것의 갯수( ball갯수에 포함되므로 그만큼 빼주기)
if (answer[i] == question[i]){
strike++;
ball--;
}
}
printf("strike : %d , ball : %d ----- %d 번째 도전\n",strike,ball,count); // 결과 출력
printf("\n");
if (strike == 3) { // strike가 3개면 while문 빠져나가기
printf("정답입니다!\n");
break;
}
count++; // 시도 횟수 +1
}
return 0;
}
'C Programming' 카테고리의 다른 글
sumMatrix.c( 다차원배열의 원소의 합) (0) | 2023.08.02 |
---|---|
lotto.c (로또 번호 생성) (0) | 2023.08.02 |
dice.c(주사위 값 srand함수) (0) | 2023.08.02 |
dice.c (주사위 값 rand함수) (0) | 2023.08.02 |
bubbleSorting.c ( 배열 버블정렬) (0) | 2023.08.02 |