Advertisement
islomiddin

Tidy Numbers/codejam2017qualB

Apr 12th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.19 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3. public class Main {
  4.     public static void main(String[] args) {
  5.         InputStream inputStream = System.in;
  6.         OutputStream outputStream = System.out;
  7.         InputReader in = new InputReader(inputStream);
  8.         //PrintWriter out = new PrintWriter(outputStream);
  9.         Solver solver = new Solver();
  10.         try {
  11.             PrintWriter out = new PrintWriter("B-large-practice.out");
  12.             solver.solve(in, out);
  13.             out.close();
  14.         } catch (IOException e) {
  15.             throw new RuntimeException(e);
  16.         }
  17.     }
  18.     static class Solver {
  19.         public void solve(InputReader in, PrintWriter out) {
  20.             int t = in.nextInt();
  21.             for (int i = 0; i < t; ++i) {
  22.                 long n = in.nextLong();
  23.                 String s = "" + n;
  24.                 int[] c = new int[s.length()];
  25.                 for (int j = 0; j < s.length(); ++j) {
  26.                     c[j] = Character.getNumericValue(s.charAt(j));
  27.                 }
  28.                 while (!ok(c)) {
  29.                     c = change(c);
  30.                 }
  31.                 out.print("Case #" + (i + 1) + ": ");
  32.                 for (int j = 0; j < c.length; ++j) {
  33.                     if (j == 0 && c[j] == 0) {
  34.                         continue;
  35.                     } else {
  36.                         out.print(c[j]);
  37.                     }
  38.                 }
  39.                 out.println();
  40.             }
  41.         }
  42.         static int[] change(int[] c) {
  43.             for (int j = 1; j < c.length; ++j) {
  44.                 if (c[j] < c[j - 1]) {
  45.                     --c[j - 1];
  46.                     for (int k = j; k < c.length; ++k) {
  47.                         c[k] = 9;
  48.                     }
  49.                 }
  50.             }
  51.             return c;
  52.         }
  53.         static boolean ok(int[] c) {
  54.             for (int i = 1; i < c.length; ++i) {
  55.                 if (c[i] < c[i - 1]) {
  56.                     return false;
  57.                 }
  58.             }
  59.             return true;
  60.         }
  61.     } // wubba lubba dub dub
  62.     static class InputReader {
  63.         public BufferedReader reader;
  64.         public StringTokenizer tokenizer;
  65.         public InputReader(InputStream stream) {
  66.             //reader = new BufferedReader(new InputStreamReader(stream), 32768);
  67.             try {
  68.                 reader = new BufferedReader(new FileReader("B-large-practice.in"));
  69.             } catch(IOException e) {
  70.                 throw new RuntimeException(e);
  71.             }
  72.             tokenizer = null;
  73.         }
  74.         public String next() {
  75.             while (tokenizer == null || !tokenizer.hasMoreElements()) {
  76.                 try {
  77.                     tokenizer = new StringTokenizer(reader.readLine());
  78.                 } catch (IOException e) {
  79.                     throw new RuntimeException(e);
  80.                 }
  81.             }
  82.             return tokenizer.nextToken();
  83.         }
  84.         public int nextInt() {
  85.             return Integer.parseInt(next());
  86.         }
  87.         public long nextLong() {
  88.             return Long.parseLong(next());
  89.         }
  90.         public double nextDouble() {
  91.             return Double.parseDouble(next());
  92.         }
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement