Isaac87 2014. 12. 6. 10:28


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

int main(void)
{
 int comNumber, inputNumber;
 int num1 = 0, num2 = 0, num3 = 0;
 // TODO : 변수 생성
 int unum1 = 0, unum2 = 0, unum3 = 0;
 int strike = 0, ball = 0;

 srand((unsigned int) time(NULL));

 do {
  comNumber = rand() % 1000;

  printf("%d\n", comNumber);

  num1 = comNumber / 100; // 523 -> 523/100 = 5
  num2 = (comNumber % 100) / 10; // (523 % 100 = 23) / 10 = 2
  num3 = comNumber % 10; // 523 % 10 = 3
 } while (num1 == num2 || num2 == num3 || num1 == num3);

 do {
  strike = ball = 0;

  scanf_s("%d", &inputNumber);

  unum1 = inputNumber / 100; // 523 -> 523/100 = 5
  unum2 = (inputNumber % 100) / 10; // (523 % 100 = 23) / 10 = 2
  unum3 = inputNumber % 10; // 523 % 10 = 3

  if (num1 == unum1)
   strike++;
  if (num2 == unum2)
   strike++;
  if (num3 == unum3)
   strike++;
  if (num1 == unum2 || num1 == unum3)
   ball++;
  if (num2 == unum1 || num2 == unum3)
   ball++;
  if (num3 == unum1 || num3 == unum2)
   ball++;
  printf("%dstrike %dball\n", strike, ball);
 } while (inputNumber != comNumber);

 printf("정답 : %d\n", comNumber);
 
 return 0;
}