Aracade Intro #56 digitsProduct. Algorithm, 알고리즘, Codefights, CodeSignal, 코드파이트, 코드시그널, 예제,문제해결능력,example, c++ java c# scalar
문제가 너무 그지같은데.. 풀긴 풀었었으나, 다시 보니까 다시 깔끔하게 정리할 필요가 없을 것 같아서 그냥 풀 당시에 쓴 처리과정 그대로 옮겨넣어둠.
It was the worst question ever, so I didn't solve again and order it. Just copied solution and process i did before.
Q.
Given an integer product, find the smallest positive (i.e. greater than 0) integer the product of whose digits is equal to product. If there is no such integer, return -1 instead.
주어진 정수곱에서, 각 자릿수의 곱이 해당 정수곱이 되는 최소값을 찾아라. 없으면 -1 반환한다.
e.g.
Input -> product = 12
Output -> digitsProduct(product) = 26
Input -> product = 19
Output -> digitsProduct(product) = -1
//Process
// 1. Input product integer // Exception -> 0, 1 or Prime
// 2. Iterate while product is less than 10
// 2.1. Iterate 2 to 9 && flag
// 2.1.1. Divide product by number
// 2.1.2. Check if remainder is 0
// 3.1.2.1. If so -> quotient is product; add number to string
// 4. Add product to string
// 5. Return Integer string
//
// //1. product를 입력받는다.
//2. 9에서 2까지 반복한다.
// 2.1. 약수인지 확인해서
// 2.1.1. 맞으면 string에 더해둔다.
// 2.1.2. 맞으면 product를 몫으로 치환한다.
//3. 소수이면 -1;
//4. 0 이면 10;
//
Code.. Lemme see code!!!!!
코드.. 코드를 보자!!!!!
int digitsProduct(int product) {
String intString = "";
String answerString = "";
boolean isPrime = false;
if (product == 1)
{
answerString = "1";
}
else if (product == 0)
{
answerString = "10";
}
while (product > 1 && !isPrime)
{
boolean isDone = false;
for (int i = 9; !isDone && i >= 2; --i)
{
int quotient = product / i;
int remainder = product - (quotient * i);
// System.out.println(quotient + " " + remainder);
if (remainder == 0)
{
product = quotient;
intString += Integer.toString(i);
isDone = true;
}
}
if (!isDone)
{
isPrime = true;
}
}
if (isPrime)
{
answerString = "-1";
}
else
{
for (int j = intString.length() - 1; j >= 0; --j)
{
answerString += intString.charAt(j);
}
}
return Integer.parseInt(answerString);
}
private String reverseString(String originString) {
String answerString = "";
for (int j = originString.length() - 1; j >= 0; --j)
{
answerString += originString.charAt(j);
}
return answerString;
}
Something else you might like..
2018/12/26 - [Programming/Design Pattern ] - Perfecting OO's Small Classes and Short Methods. 완벽한 객체지향의 작은 클래스와 짧은 메소드, Book:ThoughtWorks Anthology, Java,cpp,자바,oop,좋은코드,객체지향프로그래밍
2019/01/02 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #52 longestWord. Algorithm, 알고리즘, Codefights, CodeSignal, 코드파이트, 코드시그널, 예제, example, c++ java c# scalar
2019/01/01 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #51 deleteDigit. Algorithm, 알고리즘, Codefights, CodeSignal, 코드파이트, 코드시그널, 예제, example, c++ java c# scalar
2018/12/31 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #50 chessKnight. Algorithm, 알고리즘, Codefights, CodeSignal, 코드파이트, 코드시그널, 예제, example, c++ java c# scalar
2018/12/28 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #49 lineEncoding. Algorithm, 알고리즘 문제풀이, Codefights, CodeSignal, 코드파이트, 코드시그널, 예제, example, c++ java c# scalar
2018/12/26 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #48 isDigit. Algorithm, 알고리즘, Codefights, CodeSignal, 코드파이트, 코드시그널, 예제, example, c++ java c# scalar
2018/12/26 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #47 isMAC48Address. Algorithm, 알고리즘, Codefights, CodeSignal, 코드파이트, 코드시그널, 예제, example, c++ java c# scalar
2018/12/25 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #46 electionsWinners. Algorithm, 알고리즘, Codefights, CodeSignal, 코드파이트, 코드시그널, 예제, example, c++ java c# scalar
2018/12/21 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #45 buildPalindrome. Algorithm, 알고리즘, Codefights, CodeSignal, 코드파이트, 코드시그널, 예제, example, c++ java c# scalar
2018/12/20 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #44 findEmailDomain. Algorithm, 알고리즘, Codefights, CodeSignal, 코드파이트, 코드시그널, 예제, example, c++ java c# scalar
2018/12/17 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #43 isBeautifulString. Algorithm, 알고리즘, Codefights, CodeSignal, 코드파이트, 코드시그널, 예제, example, c++ java c# scalar
2018/12/16 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #42 bishopAndPawn. Algorithm, 알고리즘, Codefights, CodeSignal, 코드파이트, 코드시그널, 예제, example, c++ java c# scalar
2018/12/14 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #41 digitDegree. Algorithm, 알고리즘, Codefights, CodeSignal, 코드파이트, 코드시그널, 예제, example, c++ java c# scalar
2018/12/12 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #40 longestDigitsPrefix. Algorithm, 알고리즘, Codefights, CodeSignal, 코드파이트, 코드시그널, 예제, example, c++ java c# scalar
2018/12/10 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #39 knapsackLight. Algorithm, 알고리즘, Codefights, CodeSignal, 코드파이트, 코드시그널, 예제, example, c++ java c# scalar
2018/12/20 - [Programming/Java] - How to convert file to byte array (byte[]) example, 파일 객체 바이트로 변환하기 예제코드, java/cpp/c++/scalar
2018/12/18 - [Programming/Java] - How to access controller from existing Pane in JavaFX, pane에서 해당 controller로 접근하기, java/cpp/c++/fx/gui
2018/12/13 - [Computer/Linux] - How to check ongoing live process on Linux cmd, 리눅스 커맨드창에서 어떤 프로세스가 실행중인지 확인하기, java, ubuntu, cpp
2018/12/27 - [Life/Health care] - Milk-Thistle 밀크시슬 usage/side effects/dosage/fatigue/supplement,효능/부작용/성인,소아 용법/건강/피로회복/영양제
2018/12/16 - [Life/Health care] - L-Arginine 아르기닌 usage/side effects/dosage 효능/부작용/성인,소아 용법(2)
2018/12/06 - [Life/Health care] - Vitamin C 비타민 씨 usage/side effects/dosage 용법/효능/부작용
2018/12/02 - [Life/Health care] - Maca 마카 usage/side effects/dosage 효능/부작용/용법