forked from 2021-Algorithm/algorithms_site
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Complete 124country_number(124나라의숫자)
- Loading branch information
Showing
2 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package level2; | ||
|
||
public class Country124Number { | ||
public static void main(String[] args) { | ||
Country124Number country124Number = new Country124Number(); | ||
System.out.println(country124Number.solution(18)); //124 | ||
} | ||
|
||
public String solution(int n) { | ||
int[] nums = new int[]{1, 2, 4}; | ||
StringBuilder answer = new StringBuilder(); | ||
|
||
while(n>0) { | ||
n -= 1; | ||
answer = answer.insert(0, nums[(n%3)]); | ||
n /= 3; | ||
} | ||
|
||
return answer.toString(); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
def solution(n): | ||
nums = [1, 2, 4] | ||
sum = 0 | ||
## 자리수 구하기 i | ||
for i in range(1, 19): | ||
sum += 3**i | ||
if(sum >= n): | ||
break | ||
## 자리수 j | ||
j = i | ||
## 해당 자리 수의 몇 번째 수 k | ||
k = n - (sum - 3**i) | ||
|
||
ans = ['4'] * j | ||
|
||
|
||
for i in range(j): | ||
m, n = divmod(k, (3**(j-1-i))) | ||
if n == 0 : | ||
m -= 1 | ||
ans[i] = str(nums[m]) | ||
break | ||
else : | ||
ans[i] = str(nums[m]) | ||
k = n | ||
|
||
|
||
answer = ''.join(ans) | ||
return answer | ||
|
||
def solution2(n): | ||
num = ['1','2','4'] | ||
answer = '' | ||
|
||
while n > 0 : | ||
n -= 1 | ||
answer = num[n%3] + answer | ||
n //= 3 | ||
|
||
return answer | ||
|
||
|
||
print(solution(1)) | ||
print(solution(2)) | ||
print(solution(3)) | ||
print(solution(4)) | ||
print(solution(5)) | ||
print(solution(6)) | ||
print(solution(7)) | ||
print(solution(8)) | ||
print(solution(9)) | ||
print(solution(18)) ##124 | ||
|
||
|