본문 바로가기
algorithm/hackerRank

[HackerRank] Encryption

by 무대포 개발자 2020. 7. 10.
728x90
반응형

1. Problem

2. Feedback

  • 주어진 문자열을 암호화해서 출력하는 문제
  • 제약사항
    • rows * columns >= L
    • rows * columns = 최소 면적
    • 루트 L 의 버림 값 <= row <= column <= 루트 L의 올림 값
  • 핵심은 rows, columns 를 구하는 것이다.
  • rows, columns 를 구했다면, 주어진 문자열의 특정 문자열부터 columns 만큼 더해서 출력하는 것이다.

3. Source

public class Encryption {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String s = in.next();
solve(s);
}
public static void solve(String s) {
int len = s.length();
double rootLen = Math.sqrt(len);
int ip = (int) rootLen;
double fp = rootLen - ip;
int rows, columns = 0;
if (fp > 0) {
rows = ip;
columns = ip + 1;
while (rows * columns < len) {
rows = rows + 1;
}
} else if (fp == 0) {
rows = columns = ip;
}
int start = 0;
while (start < columns) {
for (int i = start; i < len; i = i + columns) {
System.out.print(s.charAt(i));
}
start++;
System.out.print(" ");
}
}
}
view raw Encryption.java hosted with ❤ by GitHub

'algorithm > hackerRank' 카테고리의 다른 글

Hackerrank Bigger is greater  (0) 2020.09.14
hackerrank A Very BigSum  (0) 2020.09.13
[HackerRank] Climbing the Leaderboard  (0) 2020.07.10
[HackkerRank] Sherlock and Anagrams  (0) 2018.10.12
[HackerRank] Minimum Swap2  (0) 2018.10.11

댓글