blog.itcode.devblog.itcode.dev

[Programmers / JAVA] Level 1 Reverse Base 3 (68935)

A natural number n is given as a parameter. Complete the solution function so that it reverses n in base 3, then returns the result expressed again in base 10.

[Programmers / JAVA] Level 1 Reverse Base 3 (68935)

A natural number n is given as a parameter. Complete the solution function so that it reverses n in base 3, then returns the result expressed again in base 10.
RWB0104
@RWBwritten at 2021-12-15 12:48:12
Programmers

시리즈 모아보기

Programmers

20 / 78
RankLanguage Used
Level 1

🖼️ JAVA

🔗 Reverse Base 3

A natural number n is given as a parameter. Complete the solution function so that it reverses n in base 3, then returns the result expressed again in base 10.

  • n is a natural number between 1 and 100,000,000.
nresult
457
125229

Input/Output Example #1

The process for deriving the answer is as follows.

n (base 10)n (base 3)Reversed (base 3)Expressed in base 10
45120000217

So it must return 7.

Input/Output Example #2

The process for deriving the answer is as follows.

n (base 10)n (base 3)Reversed (base 3)Expressed in base 10
1251112222111229

So it must return 229.

This algorithm converts an arbitrary number to base 3, arranges it in reverse order, and converts it back to base 10.

  1. Base 10 -> base 3 conversion algorithm
  2. Base 3 -> base 10 conversion algorithm

It seems best to build conversion modules so that base 10 and base 3 can be freely converted between each other.

Also, since the digits need to be arranged in reverse order in base 3, I plan to assign each digit individually into a List and reverse it all at once.


The method to convert from base 10 to base 3 is as follows.

JAVA

private ArrayList<Integer> dec2ter(int num)
{
	ArrayList<Integer> list = new ArrayList<>();
	
	int max = 0;
	
	while (Math.pow(3, max) <= num)
	{
		max++;
	}
	
	for (int i = max - 1; i > -1; i--)
	{
		int pow = (int) Math.pow(3, i);
		
		list.add(num / pow);
		
		num %= pow;
	}
	
	return list;
}

Representing base 3 as an integer doesn't carry much meaning. If a 0 comes at the front, it's very likely to be lost once converted to an integer type, and there's no real need for arithmetic on the integer form anyway. So it's returned as ArrayList<Integer>.

JAVA

private int ter2dec(ArrayList<Integer> nums)
{
	int flag = nums.size() - 1;
	
	int answer = 0;
	
	for (int num : nums)
	{
		answer += (int) Math.pow(3, flag) * num;
		
		flag--;
	}
	
	return answer;
}

The base 3 -> base 10 conversion algorithm is as shown above.

Get the base 3 array with dec2ter, arrange it in reverse order, and then convert it with the ter2dec method. The Collections.reverse() method can be used to reverse a Collection object like a List.

JAVA

import java.util.ArrayList;
import java.util.Collections;

/**
 * Reverse Base 3 class
 *
 * @author RWB
 * @since 2021.12.12 Sun 00:47:33
 */
class Solution
{
	/**
	 * Method that returns the answer
	 *
	 * @param n: [int] natural number
	 *
	 * @return [int] the answer
	 */
	public int solution(int n)
	{
		ArrayList<Integer> list = dec2ter(n);
		
		Collections.reverse(list);
		
		return ter2dec(list);
	}
	
	/**
	 * Method that converts base 10 -> base 3 array
	 *
	 * @param num: [int] natural number
	 *
	 * @return [ArrayList] base 3 array
	 */
	private ArrayList<Integer> dec2ter(int num)
	{
		ArrayList<Integer> list = new ArrayList<>();
		
		int max = 0;
		
		while (Math.pow(3, max) <= num)
		{
			max++;
		}
		
		for (int i = max - 1; i > -1; i--)
		{
			int pow = (int) Math.pow(3, i);
			
			list.add(num / pow);
			
			num %= pow;
		}
		
		return list;
	}
	
	/**
	 * Method that converts base 3 -> base 10
	 *
	 * @param nums: [ArrayList] base 3 array
	 *
	 * @return [int] base 10 number
	 */
	private int ter2dec(ArrayList<Integer> nums)
	{
		int flag = nums.size() - 1;
		
		int answer = 0;
		
		for (int num : nums)
		{
			answer += (int) Math.pow(3, flag) * num;
			
			flag--;
		}
		
		return answer;
	}
}
# Programmers# Algorithm# JAVA# Level 1
ship
blog.itcode.dev

Notes from the π-th Alpaca

7.0.1
Developed by RWB since 2021.057th upgraded at 2026.08