๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm/ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

[Java] ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค - 3์ง„๋ฒ• ๋’ค์ง‘๊ธฐ (์ •๋‹ต / ์„ค๋ช…)

by yunamom 2022. 3. 11.
๋ฐ˜์‘ํ˜•

๐Ÿ’ก๋ฌธ์ œ ์„ค๋ช…

์ž์—ฐ์ˆ˜ n์ด ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.

n์„ 3์ง„๋ฒ• ์ƒ์—์„œ ์•ž๋’ค๋กœ ๋’ค์ง‘์€ ํ›„,
์ด๋ฅผ ๋‹ค์‹œ 10์ง„๋ฒ•์œผ๋กœ ํ‘œํ˜„ํ•œ ์ˆ˜๋ฅผ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

 

์ž…์ถœ๋ ฅ ์˜ˆ

45 7
125 229

 

์ž…์ถœ๋ ฅ ์˜ˆ #1

  • n (10์ง„๋ฒ•)n (3์ง„๋ฒ•)์•ž๋’ค ๋ฐ˜์ „(3์ง„๋ฒ•)10์ง„๋ฒ•์œผ๋กœ ํ‘œํ˜„
45 1200 0021 7
  • ๋”ฐ๋ผ์„œ 7์„ return ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

์ž…์ถœ๋ ฅ ์˜ˆ #2

  • n (10์ง„๋ฒ•)n (3์ง„๋ฒ•)์•ž๋’ค ๋ฐ˜์ „(3์ง„๋ฒ•)10์ง„๋ฒ•์œผ๋กœ ํ‘œํ˜„
125 11122 22111 229
  • ๋”ฐ๋ผ์„œ 229๋ฅผ return ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป์ œ์ถœํ•œ ์ •๋‹ต์ฝ”๋“œ

class Solution {
    public int solution(int n) {
        String answer = "";
        while(n > 0){
            answer += n%3; n/=3;
        }
        return Integer.parseInt(answer,3);
    }
} // n์ด 45์ผ๋•Œ -> 0021

์‚ฌ์‹ค๋’ค์ง‘๊ธฐ๋Š” ๋ฌธ์ž์—ด์— ๊ณ„์† ๋”ํ•ด์ค€ ๊ทธ ์ˆซ์ž๋ฅผ ๋ฐ”๋กœ ์ ์šฉํ•˜๋ฉด๋˜์„œ ๊ฐ„๋‹จํ•˜๋‹ค. 

 


๐Ÿ’ก๊ทธ๋ ‡๋‹ค๋ฉด ๋’ค์ง‘๊ธฐ๊ฐ€ ์•„๋‹Œ 10์ง„์ˆ˜ -> 3์ง„์ˆ˜ -> 10์ง„์ˆ˜๋ฅผ ๊ตฌํ•˜๋ ค๋ฉด ?

class Solution {
    public int solution(int n) {
        String answer = "";
        while(n > 0){
            answer = n%3+answer; // <- ์ด๋ถ€๋ถ„๋งŒ ๋ณ€๊ฒฝํ•ด์ฃผ๋ฉด๋œ๋‹ค.
            n/=3;
        }
        return Integer.parseInt(answer,3);
    }
} // n์ด 45์ผ๋•Œ -> 1200

 

์—ฌ๊ธฐ์„œ Integer.parseInt(answer,3) ์ด ํ•จ์ˆ˜๋Š” ์–ด๋–ป๊ฒŒ ๋งŒ๋“ค์–ด์ง€๋Š”๊ฑธ๊นŒ?

 

์ด ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์ง€ ์•Š๊ณ  ๊ฐ’์„ ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋ฌด์—‡์ผ๊นŒ ?

public int solution(int num , int x) { 
	
		String s = "";		
		while(num > 0) {  
		    s+=num%x;
		    num/=x;	     
		}
			
		return Integer.parseInt(s); // num์ด 45์ผ๋•Œ -> 21
	}

 

 

public int solution2(int num , int x) {
		 int answer = 0;
		 int cnt = 0;
	
		 while(num > 0) {
			 answer += (int) (num%10 * (Math.pow(x,cnt))); 
			 cnt++; 
			 num/=10;
		 }
		  return answer; // num์ด 45์ผ๋•Œ -> 7
	  }

 

10์ง„์ˆ˜ : 45   -> 3 ์ง„์ˆ˜ : 21

3 ์ง„์ˆ˜ : 21    -> 10์ง„์ˆ˜ : 7

 

45 ๋ฅผ 3์ง„์ˆ˜๋กœ ๋ณ€ํ™˜ํ•œํ›„ ๋’ค์ง‘์€ ์ˆซ์ž 21 ์„ 10์ง„์ˆ˜ ๋กœ ๋ณ€ํ™˜ํ•˜์˜€๋‹ค.

 

๋’ค์ง‘์ง€ ์•Š๊ณ  10 ์ง„์ˆ˜ -> 3 ์ง„์ˆ˜ -> 10์ง„์ˆ˜๋กœ ๋ณ€ํ™˜ํ•˜๋ ค๋ฉด?

public int solution(int num , int x) { 
	
		String s = "";		
		while(num > 0) {  
		    s=num%x+s; // <-๐Ÿ’ก์ด๋ถ€๋ถ„๋งŒ ๋ณ€๊ฒฝํ•ด์ฃผ๋ฉด๋œ๋‹ค.
		    num/=x;	     
		}
			
		return Integer.parseInt(s); // num์ด 45์ผ๋•Œ -> 1200
	}

solution2 ์˜ ๋ถ€๋ถ„์„ ์ž์„ธํžˆ ๋ด๋ณด์ž.

public int solution2(int num , int x) {
		 int answer = 0;
		
		/* for(int i=s.length()-1; i>=0; i--) {
			  answer += ((int)s.charAt(i) - '0') * (Math.pow(x,(s.length()-1)-i));
		  }*/
          
         //์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์œผ๋กœ ์ž‘์„ฑํ• ์ˆ˜ ์žˆ๋‹ค. ์œ„์˜๊ฒฝ์šฐ String ์œผ๋กœ ๋ฐ›์•˜์„๋•Œ ์•„๋ž˜์˜ ๊ฒฝ์šฐ๋Š” int ๋กœ ๊ฐ’์„ ๋ฐ›์€๊ฒฝ์šฐ
		 int cnt = 0;
         
		 while(num > 0) {
			 answer += (int) (num%10 * (Math.pow(x,cnt))); cnt++;	
			 num/=10;
		 }
		  return answer;
	  }

Math.pow(x,cnt) <- x ์˜ cnt ์ œ๊ณฑ์„ ๊ตฌํ•œ๋‹ค. cnt <- ์ž๋ฆฟ์ˆ˜

 

java.lang.Math ํด๋ž˜์Šค๋Š” ์ˆ˜ํ•™ ๊ณ„์‚ฐ์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ์ œ๊ณตํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

Math ํด๋ž˜์Šค๊ฐ€ ์ œ๊ณตํ•˜๋Š” ๋ฉ”์†Œ๋“œ๋Š” ๋ชจ๋‘ ์ •์ ์ด๋ฏ€๋กœ Import๋‚˜ Mathํด๋ž˜์Šค ์„ ์–ธ ์—†์ด ๋ฐ”๋กœ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

Math.pow()๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฑฐ๋“ญ์ œ๊ณฑ์„ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

pow๋Š” power๋ฅผ ์˜๋ฏธํ•˜๊ณ , power์˜ ๋œป์€ ๊ฑฐ๋“ญ์ œ๊ณฑ์ž…๋‹ˆ๋‹ค.

pow() ์ธ์ž๋กœ a์™€ b๋ฅผ ์ „๋‹ฌํ•˜๋ฉฐ, a์˜ b ์ œ๊ณฑ์ด ๋ฆฌํ„ด๋ฉ๋‹ˆ๋‹ค.

์ฆ‰, a๋ฅผ b๋ฒˆ ๊ณฑํ•œ ๊ฐ’์ด ๋ฆฌํ„ด๋ฉ๋‹ˆ๋‹ค.

 

์ „์ฒด์ฝ”๋“œ 

package reverse3์ง„๋ฒ•;

class SolutionB{
	
	  public int solution2(int num , int x) {
		 int answer = 0;
		 int cnt = 0;
	
		 while(num > 0) {
			 answer += (int) (num%10 * (Math.pow(x,cnt))); 
			 cnt++; 
			 num/=10;
		 }
		  return answer;
	  }
	
	public int solution(int num , int x) { 
	
		String s = "";		
		while(num > 0) {  
		    s=num%x+s; 
      		// s+=num%x; <- ๋’ค์ง‘๊ธฐ์ธ๊ฒฝ์šฐ
		    num/=x;	     
		}
			
		return Integer.parseInt(s);
	}
}
public class BinaryNumber_A_to_B_to_C {
	
	public static void main(String[] args) {

		 	int num = 45; 
			int x = 3; //์ง„์ˆ˜ ๋ณ€์ˆ˜
		
		SolutionB test = new SolutionB();
		System.out.printf("10์ง„์ˆ˜ : %-5d -> %-2d์ง„์ˆ˜ : %d\n\n",num,x,test.solution(num, x));
		// 10์ง„์ˆ˜๋ฅผ - > x ์ง„์ˆ˜๋กœ ๋ณ€ํ™˜
		int binary_x = test.solution(num, x);
		
		System.out.printf("%-2d์ง„์ˆ˜ : %-5s -> 10์ง„์ˆ˜ : %d\n\n",x,binary_x,test.solution2(binary_x, x));
		
		System.out.println(test.solution2(binary_x, x));
		// x ์ง„์ˆ˜๋ฅผ -> 10 ์ง„์ˆ˜๋กœ ๋ณ€ํ™˜
	}
}

 

Forward a number
Reverse a number

 

300x250

์ฝ”๋“œ