import java.util.Scanner; public class slo { private int signs; private int wordNum; private void read() { Scanner scanner = new Scanner(System.in); this.signs = scanner.nextInt(); this.wordNum = scanner.nextInt(); } private String calculate() { int num = wordNum - 1; if (inside(num, signs)) { return findString('a', num, signs); } int treeSize = treeSize(signs); if (inside(num - treeSize, signs)) { return findString('b', num - treeSize, signs); } else if (inside(num - 2 * treeSize, signs)) { return findString('c', num - 2 * treeSize, signs); } return "NIE"; } private String findString(char rootChar, int num, int height) { String result = String.valueOf(rootChar); while (num > 0) { num -= 1; height -= 1; if (inside(num, height)) { rootChar = smallerChar(rootChar); } else { rootChar = biggerChar(rootChar); num -= treeSize(height); } result += rootChar; } return result; } private char smallerChar(char c) { if (c == 'a') { return 'b'; } return 'a'; } private char biggerChar(char c) { if (c == 'c') { return 'b'; } return 'c'; } private boolean inside(int nodeNr, int treeHeight) { int size = 0; for (int i = 0; i < treeHeight; i++) { size = size * 2 + 1; if (size > nodeNr) { return true; } } return false; } private int treeSize(int height) { if (height == 1) { return 1; } return 2 * treeSize(height - 1) + 1; } public static void main(String args[]) { slo slo = new slo(); slo.read(); System.out.println(slo.calculate()); } }
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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | import java.util.Scanner; public class slo { private int signs; private int wordNum; private void read() { Scanner scanner = new Scanner(System.in); this.signs = scanner.nextInt(); this.wordNum = scanner.nextInt(); } private String calculate() { int num = wordNum - 1; if (inside(num, signs)) { return findString('a', num, signs); } int treeSize = treeSize(signs); if (inside(num - treeSize, signs)) { return findString('b', num - treeSize, signs); } else if (inside(num - 2 * treeSize, signs)) { return findString('c', num - 2 * treeSize, signs); } return "NIE"; } private String findString(char rootChar, int num, int height) { String result = String.valueOf(rootChar); while (num > 0) { num -= 1; height -= 1; if (inside(num, height)) { rootChar = smallerChar(rootChar); } else { rootChar = biggerChar(rootChar); num -= treeSize(height); } result += rootChar; } return result; } private char smallerChar(char c) { if (c == 'a') { return 'b'; } return 'a'; } private char biggerChar(char c) { if (c == 'c') { return 'b'; } return 'c'; } private boolean inside(int nodeNr, int treeHeight) { int size = 0; for (int i = 0; i < treeHeight; i++) { size = size * 2 + 1; if (size > nodeNr) { return true; } } return false; } private int treeSize(int height) { if (height == 1) { return 1; } return 2 * treeSize(height - 1) + 1; } public static void main(String args[]) { slo slo = new slo(); slo.read(); System.out.println(slo.calculate()); } } |