summaryrefslogtreecommitdiff
path: root/2.2/runround.c
diff options
context:
space:
mode:
Diffstat (limited to '2.2/runround.c')
-rw-r--r--2.2/runround.c72
1 files changed, 72 insertions, 0 deletions
diff --git a/2.2/runround.c b/2.2/runround.c
new file mode 100644
index 0000000..498b579
--- /dev/null
+++ b/2.2/runround.c
@@ -0,0 +1,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;
+}