1// number_guess.c
2// μ«μ λ§μΆκΈ° κ²μ
3
4#include <stdio.h>
5#include <stdlib.h>
6#include <time.h>
7
8int main(void) {
9 int secret, guess, attempts;
10 int min = 1, max = 100;
11
12 // λμ μλ μ΄κΈ°ν
13 srand(time(NULL));
14
15 printf("=== μ«μ λ§μΆκΈ° κ²μ ===\n");
16 printf("1λΆν° 100 μ¬μ΄μ μ«μλ₯Ό λ§μΆ°λ³΄μΈμ!\n\n");
17
18 // 1-100 μ¬μ΄ λλ€ μ«μ μμ±
19 secret = rand() % 100 + 1;
20 attempts = 0;
21
22 while (1) {
23 printf("μ«μλ₯Ό μ
λ ₯νμΈμ (%d ~ %d): ", min, max);
24
25 if (scanf("%d", &guess) != 1) {
26 printf("μ¬λ°λ₯Έ μ«μλ₯Ό μ
λ ₯νμΈμ.\n");
27 while (getchar() != '\n'); // μ
λ ₯ λ²νΌ λΉμ°κΈ°
28 continue;
29 }
30
31 attempts++;
32
33 if (guess < min || guess > max) {
34 printf("λ²μ λ΄μ μ«μλ₯Ό μ
λ ₯νμΈμ!\n");
35 continue;
36 }
37
38 if (guess == secret) {
39 printf("\nμ λ΅μ
λλ€! π\n");
40 printf("%dλ² λ§μ λ§μΆμ
¨μ΅λλ€.\n", attempts);
41 break;
42 } else if (guess < secret) {
43 printf("λ ν° μ«μμ
λλ€.\n");
44 if (guess > min) min = guess + 1;
45 } else {
46 printf("λ μμ μ«μμ
λλ€.\n");
47 if (guess < max) max = guess - 1;
48 }
49 }
50
51 return 0;
52}