-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumDecending.java
More file actions
36 lines (35 loc) · 1.08 KB
/
Copy pathNumDecending.java
File metadata and controls
36 lines (35 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package level1;
/*
* 함수 solution은 정수 n을 매개변수로 입력받습니다. n의 각 자릿수를 큰것부터 작은 순으로 정렬한 새로운 정수를 리턴해주세요.
* 예를들어 n이 118372면 873211을 리턴하면 됩니다.
제한 조건
n은 1이상 8000000000 이하인 자연수입니다.
입출력 예
n return
118372 873211
*/
public class NumDecending {
public static void main(String[] args) {
SolutionNumDecending su = new SolutionNumDecending();
int n = 118372;
System.out.println(su.solution(n));
}
}
class SolutionNumDecending {
public long solution(long n) {
String[] str = String.valueOf(n).split("");
for(int i=0; i<str.length-1; i++) {
for(int j=i+1; j<str.length; j++) {
if(Integer.parseInt(str[i]) < Integer.parseInt(str[j])) {
String tmp = str[i];
str[i] = str[j];
str[j] = tmp;
}
}
}
String answer="";
for(String data : str)
answer += data;
return Long.parseLong(answer);
}
}