728x90
반응형
1. Problem
2. Feedback
- 주어진 문자열을 암호화해서 출력하는 문제
- 제약사항
- rows * columns >= L
- rows * columns = 최소 면적
- 루트 L 의 버림 값 <= row <= column <= 루트 L의 올림 값
- 핵심은 rows, columns 를 구하는 것이다.
- rows, columns 를 구했다면, 주어진 문자열의 특정 문자열부터 columns 만큼 더해서 출력하는 것이다.
3. Source
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(" "); | |
} | |
} | |
} |
'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 |
댓글