TIL
코드카타
Mon Groy
2024. 6. 4. 20:00
정수 제곱근 판별
|
class Solution {
public long solution(long n) {
for (long x = 1; x <= n ; x++) {
if (n/x == x) {
return (x + 1)*(x + 1);
}
}
return -1;
}
}
|
cs |
정수 내림차순으로 배치하기
|
class Solution {
public static long solution(long n) {
int length = String.valueOf(n).length();
int[] numbers = new int[length];
for (int i = 0 ; i < length ; i++ ) {
int x = (int) n % 10;
n = n/10;
numbers[i] = x;
}
Arrays.sort(numbers);
int descNumber = 0;
int j = 1;
for (int i = 0 ; i < length; i++) {
descNumber += numbers[i] * j;
j *= 10;
}
return descNumber;
}
}
|
cs |