summaryrefslogtreecommitdiff
path: root/2.2/runround.c
blob: 498b5790fc92fc31de9110cc03c491d0aed7ad6c (plain)
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
ID: mytbk921
LANG: C
TASK: runround
*/

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

int runround(unsigned int x)
{
	unsigned int msd, lsd, last, t;
	int next[10];
	int map[10];
	int i;

	for (i=0; i<10; i++) map[i] = 0;

	lsd = x % 10;
	if (lsd == 0) return 0;
	last = lsd;
	map[lsd] = 1;
	x /= 10;
	while (x>=10) {
		t = x % 10;
		if (map[t] || t==0) return 0;
		map[t] = 1;
		next[t] = last;
		last = t;
		x /= 10;
	}
	msd = x;
	map[msd] = 1;
	next[msd] = last;
	next[lsd] = msd;

	last = msd;
	do {
		if (map[last])
			map[last] = 0;
		else
			return 0;
		t = last;
		for (i=0; i<t; i++)
			last = next[last];
	} while (last != msd);
	for (i=0; i<10; i++) {
		if (map[i])
			return 0;
	}
	return 1;
}

int main()
{
	FILE *fin = fopen("runround.in", "r");
	FILE *fout = fopen("runround.out", "w");

	unsigned int m;
	fscanf(fin, "%u", &m);
	fclose(fin);

	while (1) {
		m++;
		if (runround(m)) {
			fprintf(fout, "%u\n", m);
			fclose(fout);
			return 0;
		}
	}
	return 0;
}