summaryrefslogtreecommitdiffstats
path: root/inf/liga/1.c
blob: 955cd890434c31928d4c314b9014e7c16f0954c0 (plain) (blame)
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <stdio.h>
#include <stdlib.h>
#define M 512
#define pedantic_fgets(a, b, c) if (!fgets(a, b, c)) { fprintf(stderr, "napaka pri vnosu\n"); return 1; }
int main (void) {
	char buf[M];
	char * c;
	int a, b, s;
	pedantic_fgets(buf, M, stdin);
	a = strtol(buf, &c, 10);
	b = strtol(++c, NULL, 10);
	if (a > b) {
		s = a; /* swap */
		a = b;
		b = s;
	}
	fprintf(stderr, "interval: %05d %05d\n", a, b);
	while (1) {
		int v, u;
		u = (a+b)/2; /* ugibamo to */
		fprintf(stderr, "testiram %05d\n", u);
		pedantic_fgets(buf, M, stdin);
		if (!(v = strtol(buf, NULL, 10))) {
			if ((b-a) <= 1)
				goto ogabno;
			fprintf(stdout, "%d %d\n", a, b);
			return 0; /* da se CMS ne zmede */
		}
		if ((b-a) <= 1) { /* našli smo število */
ogabno:
			if (v)
				fprintf(stdout, "PROTISLOVJE\n");
			else
				fprintf(stdout, "%d\n", u);
			return 0;
		}
		if (v == -1) {
			b = u;
			b--;
			fprintf(stderr, "-1: število je manjše od %05d, interval %05d %05d\n", u, a, b);
		}
		if (v == 1) {
			a = u;
			a++;
			fprintf(stderr, " 1: število je večje  od %05d, interval %05d %05d\n", u, a, b);
		}
	}
	return 0;
}