Beispiel #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));
 }
Beispiel #4
1
 private static String prs(String str, boolean rotate) {
   StringBuilder val = new StringBuilder(str);
   for (int i = 0; i < val.length(); ) {
     int c = val.charAt(i);
     if (c < 48 || c > 57) {
       val.deleteCharAt(i);
     } else i++;
   }
   return (rotate ? val.reverse() : val).toString();
 }
Beispiel #5
1
 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();
 }
 /*public static int prime(int n)
 {
 	int c=0;
 	for(int i=2;i<=n/2;i++)
 	{
 		if(n%i==0)
 			c++;
 	}
 	if(c==0)
 		return 1;
 	else
 		return 0;
 }*/
 public static int palin(int n) {
   StringBuilder sb = new StringBuilder(n + "");
   String r = sb.reverse().toString();
   if (r.equals(n + "")) return 1;
   else return 0;
 }
Beispiel #7
0
  private void doMain() throws SAXParseException, IOException {

    Normalizer normalizer = null;
    if (useTokenizer) {
      normalizer = Normalizer.getInstance();
    }

    PrintWriter reversedWordsWriter =
        analysisPlacesOut != null
            ? new PrintWriter(new File(analysisPlacesOut, "reversedWords.txt"))
            : new PrintWriter(System.out);

    BufferedReader bufferedReader = new BufferedReader(new FileReader(placesIn));

    int lineCount = 0;
    while (bufferedReader.ready()) {
      String nextLine = bufferedReader.readLine();
      nextLine = nextLine.trim().toLowerCase();
      if (nextLine.length() == 0) continue;

      lineCount++;
      if (lineCount % 5000 == 0) System.out.println("indexing line " + lineCount);

      placesCountCC.add(nextLine);
      totalPlacesCount++;

      String[] placeList = nextLine.split(SPLIT_REGEX);

      for (String place : placeList) {
        place = place.trim();

        if (place.length() == 0) continue;

        if (NumberUtils.isNumber(place)) {
          numbersCountCC.add(place);
          totalNumbersCount++;
        } else {
          wordsCountCC.add(place);
          totalWordsCount++;
        }
      }

      int lastCommaIndx = nextLine.lastIndexOf(",");
      String lastWord = nextLine.substring(lastCommaIndx + 1).trim();
      if (lastWord.length() > 0) {
        endingsOfPlacesCC.add(lastWord);
        endingsOfPlacesTotalCount++;
      }

      if (lineCount % REVERSE_EVERY_N == 0) {
        StringBuilder reversedWord = new StringBuilder(nextLine);
        reversedWordsWriter.println(reversedWord.reverse());
      }

      if ((useTokenizer) && (lineCount % TOKENIZE_EVERY_N == 0)) {
        List<List<String>> levels = normalizer.tokenize(nextLine);
        for (List<String> levelWords : levels) {
          tokenizerPlacesCountCC.addAll(levelWords);
          totalTokenizerPlacesCount += levelWords.size();
        }
      }
    }

    System.out.println("total number of lines in files " + lineCount);

    System.out.println("Indexed a total of " + totalPlacesCount + " places.");
    System.out.println("Found a total of " + getPlacesCountCC().size() + " unique places.");
    getPlacesCountCC()
        .writeSorted(
            false,
            1,
            analysisPlacesOut != null
                ? new PrintWriter(new File(analysisPlacesOut, "placesCount.txt"))
                : new PrintWriter(System.out));

    System.out.println("Indexed a total of " + totalWordsCount + " words.");
    System.out.println("Found a total of " + getWordsCountCC().size() + " unique words.");
    getWordsCountCC()
        .writeSorted(
            false,
            1,
            analysisPlacesOut != null
                ? new PrintWriter(new File(analysisPlacesOut, "wordsCount.txt"))
                : new PrintWriter(System.out));

    System.out.println("Indexed a total of " + totalNumbersCount + " numbers.");
    System.out.println("Found a total of " + getNumbersCountCC().size() + " unique numbers.");
    getNumbersCountCC()
        .writeSorted(
            false,
            1,
            analysisPlacesOut != null
                ? new PrintWriter(new File(analysisPlacesOut, "numbersCount.txt"))
                : new PrintWriter(System.out));

    System.out.println("Indexed a total of " + endingsOfPlacesTotalCount + " endings.");
    System.out.println("Found a total of " + getEndingsOfPlacesCC().size() + " unique endings.");
    getEndingsOfPlacesCC()
        .writeSorted(
            false,
            1,
            analysisPlacesOut != null
                ? new PrintWriter(new File(analysisPlacesOut, "endingsCount.txt"))
                : new PrintWriter(System.out));

    if (useTokenizer) {
      System.out.println("Indexed a total of " + totalTokenizerPlacesCount + " normalized words.");
      System.out.println(
          "Found a total of " + getTokenizerPlacesCountCC().size() + " normalized words.");
      getTokenizerPlacesCountCC()
          .writeSorted(
              false,
              1,
              analysisPlacesOut != null
                  ? new PrintWriter(new File(analysisPlacesOut, "normalizedWordsCount.txt"))
                  : new PrintWriter(System.out));
    }
  }