about summary refs log tree commit diff
path: root/others/coastline/dig.c
blob: 8770a6b099fc64a7f0b2d658e08e24c334a63677 (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 <string.h>

int main()
{
	char k, n[21], digits[10] = {}, i, avail = 0, j;

	scanf("%hhd %s", &k, n);

	for (i = 0; i < strlen(n); i++)
		digits[n[i] - 48] = 1;

	for (i = 0; i < 10; i++)
		if (!digits[i])
			avail++;

	if (avail == 1 && !digits[0] || !avail || avail > k) {
		putchar(48);
	} else {
		if (!digits[0])
			for (i = 1; i < 10; i++)
				if (!digits[i]) {
					putchar(i + 48);
					digits[i] = 1;
					avail--;
					k--;
					break;
				}

		for (i = 0; i < 10 && k; i++)
			if (!digits[i]) {
				for (j = 0; j <= k - avail; j++)
					putchar(i + 48);
				k = avail - 1;
				digits[i] = 1;
				break;
			}

		for (i = 0; i < 10 && k; i++)
			if (!digits[i]) {
				putchar(i + 48);
				k--;
			}
	}

	putchar(10);

	return 0;
}