예제 #1
1
 public static void main(String[] args) {
   Scanner scanner = new Scanner(System.in);
   int m = scanner.nextInt();
   assert 1 <= m && m <= 100 : "out of range, m: " + m;
   int s = scanner.nextInt();
   assert 0 <= s && s <= 900 : "out of range, s: " + s;
   if (s > 9 * m || (s == 0 && m > 1)) {
     System.out.println("-1 -1");
     return;
   }
   if (m == 1 && s == 0) {
     System.out.println("0 0");
     return;
   }
   StringBuilder sb = new StringBuilder();
   int l = 0;
   for (int i = 0; i < m; i++) {
     int d = (s >= 9) ? 9 : s;
     sb.append(d);
     s -= d;
     if (d != 0) {
       l = i;
     }
   }
   String large = sb.toString();
   if (sb.charAt(m - 1) == '0') {
     sb.setCharAt(l, (char) (sb.charAt(l) - 1));
     sb.setCharAt(m - 1, '1');
   }
   String small = sb.reverse().toString();
   System.out.printf("%s %s", small, large);
 }
  public static String reverse_hash(long hash) {

    StringBuilder code = new StringBuilder(9);
    String letters = "acdegilmnoprstuw";

    int index = 0;

    // Since the hash is initialized with a value of 7 and then built up from there, we iterate
    // and subtract values corresponding to characters until we hit the base case (7).
    while (hash > 7) {

      if (hash % 37 == 0) {

        // current hash value is a multiple of 37. We hit a character in the string.
        code.append(letters.charAt(index));
        index = 0;
        hash /= 37;

      } else {

        // Subtract by one and move to the next character.
        hash -= 1;
        index += 1;
      }
    }

    return code.reverse().toString();
  }
 public static void main(String[] args) {
   String input = "AliveisAwesome";
   StringBuilder input1 = new StringBuilder();
   input1.append(input);
   input1 = input1.reverse();
   for (int i = 0; i < input1.length(); i++) System.out.print(input1.charAt(i));
 }
예제 #4
1
파일: Encrypt.java 프로젝트: ksp499/cs337
 public static final String toBinary(long integer) {
   StringBuilder builder = new StringBuilder();
   long temp = 0;
   while (integer > 0) {
     temp = integer;
     integer = (temp >> 1);
     builder.append(temp % 2);
   }
   while (builder.length() < 32) builder.append("0");
   return builder.reverse().toString();
 }
예제 #5
1
 public static void main(String[] a) throws Exception {
   byte[] b = new byte[20000];
   System.in.read(b);
   String[] w = new String(b).split("\\s");
   StringBuilder x = new StringBuilder(6000), c = new StringBuilder();
   int n = Integer.parseInt(w[0]), i, j;
   Arrays.sort(w, 1, n + 1);
   Set<String> t = new HashSet<>();
   for (i = 1; i <= n; i++) t.add(w[i]);
   for (i = 1; i <= n; i++) {
     String v = w[i], r = new StringBuilder(v).reverse().toString();
     if (r.compareTo(v) == 0) c.append(r);
     else if (t.contains(r) && v.compareTo(r) < 0) x.append(v);
   }
   System.out.write((x.toString() + c + x.reverse()).getBytes());
 }
예제 #6
1
  private String GetPath(
      Map<Integer, List<Integer>> currentGraph, Integer currentNode, Integer connectedNode) {
    int[] parents = new int[currentGraph.size() + 1];
    Queue<Integer> queue = new LinkedList<>();
    int[] visited = new int[currentGraph.size() + 1];

    queue.add(currentNode);
    visited[currentNode] = 1;
    parents[currentNode] = 0;

    while (!queue.isEmpty()) {
      int node = queue.remove();

      if (node == connectedNode) {
        break;
      }

      for (Integer childNode : currentGraph.get(node)) {
        if (visited[childNode] == 0) {
          queue.add(childNode);
          parents[childNode] = node;
          visited[childNode] = 1;
        }
      }
    }

    if (parents[connectedNode] != 0) {
      StringBuilder sb = new StringBuilder();

      int j = connectedNode;

      while (j != 0) {
        sb.append(" ");
        sb.append(j);
        j = parents[j];
      }

      sb.reverse();

      return sb.toString();
    } else {
      return "";
    }
  }
  /**
   * Apply DFS to the digraph to get the topological sort result.
   *
   * @param characters
   * @param dependencyList
   * @return
   */
  private String alienOrderDFS(
      HashSet<Character> characters, ArrayList<ArrayList<Character>> dependencyList) {
    boolean[] visited = new boolean[26];
    boolean[] onStack = new boolean[26];

    for (int i = 0; i < 26; ++i) {
      visited[i] = false;
      onStack[i] = false;
    }

    HashMap<Character, ArrayList<Character>> digraph = new HashMap<>();
    buildDigraph(dependencyList, digraph);

    StringBuilder res = new StringBuilder();
    for (char c : characters) {
      if (!visited[c - 'a']) {
        if (!dfs(c, digraph, visited, onStack, res)) return "";
      }
    }

    StringBuilder str = new StringBuilder(res);
    return str.reverse().toString();
  }
예제 #8
0
  public static void main(String[] args) {
    int num = 999;
    long product = 0;
    String stringNo;
    StringBuilder test;
    ArrayList<Integer> palindromes = new ArrayList<Integer>();

    for (int i = 100; i <= num; i++) {
      for (int a = 100; a <= num; a++) {
        product = i * a;
        stringNo = String.valueOf(product);
        test = new StringBuilder(stringNo);

        test.reverse();

        if (stringNo.equals(test.toString())) {
          palindromes.add(Integer.parseInt(stringNo));
        }
      }
    }
    System.out.println(
        "The largest palindrome made from the product of two 3-digit numbers is: "
            + Collections.max(palindromes));
  }