/* 1996 ACM North Central Regional Programming Contest Problem C -- Run, Run, Runaround Numbers Ed Karrels, October 1998 */ import ContestSolution; import java.io.*; public class acmreg96c implements ContestSolution { public static void main(String[] args) { acmreg96c soln = new acmreg96c(); soln.do_it(System.in, System.out); } public void do_it(InputStream input, PrintStream output) { StreamTokenizer token_input = new StreamTokenizer(input); int x; // the current input value int r; // the current output value being tested int case_no=1; // counter boolean is_runaround; // flag that shows whether the current // output value still appears to be // a runaround number x = read_int(token_input); while (x != 0) { is_runaround = false; // look for the first runaround number greater than // or equal to x for (r=x; !is_runaround; r++) { // put the digits of the current number into an array int[] digits = splitDigits(r); int len = digits.length; // check that no digit is repeated if (digitsAreUnique(digits)) { is_runaround = true; int pos = 0; int k = digits[0]; digits[pos] = 0; // check if x is a runaround number for (int i=1; is_runaround && i= 10) { mag *= 10; len++; } int[] digits = new int[len]; for (int dig_no=0; dig_no < len; dig_no++) { digits[dig_no] = num / mag; num -= digits[dig_no] * mag; mag /= 10; } return digits; } // read an integer from the given StreamTokenizer int read_int(StreamTokenizer st) { try { st.nextToken(); if (st.ttype != StreamTokenizer.TT_NUMBER) st.nval = 0; } catch (IOException e) { st.nval = 0; } return (int)st.nval; } }