[알고리즘] 백준 2588번 곱셈 자바(JAVA)

2021. 1. 17. 00:06백준알고리즘

728x90

 

[문제]

(세 자리 수) × (세 자리 수)는 다음과 같은 과정을 통하여 이루어진다.

(1)과 (2)위치에 들어갈 세 자리 자연수가 주어질 때 (3), (4), (5), (6)위치에 들어갈 값을 구하는 프로그램을 작성하시오.

 

 

[나의 해결방법]

1단계 :  각 자리수의 곱셈을 구하고, 각 자리수의 곱셈결과값이 2자리일 경우는 잘라서 앞자리는 반올림해줌.

2단계 :  (3),(4),(5) 세 개의 각 결과값을 동일한 자릿수로 만들기 위해 최대 자릿수를 기준으로 빈 공간에 '0'을 대입해줌.

3단계 :  2단계를 거쳐 나온 최대 자리수만큼의 배열 한 개를 생성하여 해당 배열 각 인덱스 마다 덧셈을 구할 (3),(4),(5)의 각 자리수를 담아줌.

4단계 : 3단계에서 생성된 배열의 각 인덱스별로 덧셈을 해주는데, 덧셈시 9를 넘는 결과값(2자리수)이 존재할 경우 앞 인덱스 결과값에 더해줌.(반올림)

package com.baekjoon.algorithm;
import java.util.Scanner;
public class Multiply_2589{
	public static void main(String[] args){
    	
    	Scanner sc = new Scanner(System.in);
        //입력값
    	String input1 = sc.nextLine();
    	String input2 = sc.nextLine();
    	
        //1단계
    	String input1_3 = input1.substring(2, input1.length());
    	String input2_3 = input2.substring(2, input1.length());
    	int firstResult1_3Int = Integer.parseInt(input1_3) *  Integer.parseInt(input2_3);
    	String firstResult1_3StrLast = Integer.toString(firstResult1_3Int); 
        int firstResult1_3IntOlim = 0;
    	if(firstResult1_3StrLast.length() == 2) {
            firstResult1_3IntOlim = Integer.parseInt(firstResult1_3StrLast.substring(0,1)); 
    		firstResult1_3StrLast = firstResult1_3StrLast.substring(1,firstResult1_3StrLast.length());
    	}
    	
    	String input1_2 = input1.substring(1, 2);
    	int firstResult1_2Int = Integer.parseInt(input1_2) *  Integer.parseInt(input2_3);
    	int firstResult1_2IntLast = firstResult1_2Int + firstResult1_3IntOlim ; 
    	String firstResult1_2StrLast = Integer.toString(firstResult1_2IntLast); 
        int firstResult1_2IntOlim = 0;
    	if(firstResult1_2StrLast.length() == 2) {
    		firstResult1_2IntOlim = Integer.parseInt(firstResult1_2StrLast.substring(0,1)); 
    		firstResult1_2StrLast = firstResult1_2StrLast.substring(1,firstResult1_2StrLast.length());
    	}
    	
    	String input1_1 = input1.substring(0, 1);
    	int firstResult1_1Int = Integer.parseInt(input1_1) *  Integer.parseInt(input2_3);
    	int firstResult1_1IntLast = firstResult1_1Int + firstResult1_2IntOlim ; 
    	String firstResult1_1StrLast = Integer.toString(firstResult1_1IntLast);
    
    	String firstResult1 = firstResult1_1StrLast + firstResult1_2StrLast + firstResult1_3StrLast; 
    	System.out.println(Integer.parseInt(firstResult1));
    
        String input2_2 = input2.substring(1, 2);
    	int firstResult2_3Int = Integer.parseInt(input1_3) *  Integer.parseInt(input2_2);
    	String firstResult2_3StrLast = Integer.toString(firstResult2_3Int); 
    	int firstResult2_3IntOlim = 0;
    	if(firstResult2_3StrLast.length() == 2) {
            firstResult2_3IntOlim = Integer.parseInt(firstResult2_3StrLast.substring(0,1)); 
    		firstResult2_3StrLast = firstResult2_3StrLast.substring(1,firstResult2_3StrLast.length());
    	}
    	
    	int firstResult2_2Int = Integer.parseInt(input1_2) *  Integer.parseInt(input2_2);
    	int firstResult2_2IntLast = firstResult2_2Int + firstResult2_3IntOlim ; 
    	String firstResult2_2StrLast = Integer.toString(firstResult2_2IntLast); //맨끝자리 곱한 값
    	int firstResult2_2IntOlim = 0;
    	if(firstResult2_2StrLast.length() == 2) {
    		firstResult2_2IntOlim = Integer.parseInt(firstResult2_2StrLast.substring(0,1)); 
    		firstResult2_2StrLast = firstResult2_2StrLast.substring(1,firstResult2_2StrLast.length());
    	}
    	int firstResult2_1Int = Integer.parseInt(input1_1) *  Integer.parseInt(input2_2);
    	int firstResult2_1IntLast = firstResult2_1Int + firstResult2_2IntOlim ; 
    	String firstResult2_1StrLast = Integer.toString(firstResult2_1IntLast); //맨끝자리 곱한 값
    	
    	String firstResult2 = firstResult2_1StrLast + firstResult2_2StrLast + firstResult2_3StrLast; 
        System.out.println(Integer.parseInt(firstResult2));	
    
    	String input2_1 = input2.substring(0, 1);
    	int firstResult3_3Int = Integer.parseInt(input1_3) *  Integer.parseInt(input2_1);
    	String firstResult3_3StrLast = Integer.toString(firstResult3_3Int); 
        int firstResult3_3IntOlim = 0;
    	if(firstResult3_3StrLast.length() == 2) {
            firstResult3_3IntOlim = Integer.parseInt(firstResult3_3StrLast.substring(0,1)); 
    		firstResult3_3StrLast = firstResult3_3StrLast.substring(1,firstResult3_3StrLast.length());
    	}
    	
    	int firstResult3_2Int = Integer.parseInt(input1_2) *  Integer.parseInt(input2_1);
    	int firstResult3_2IntLast = firstResult3_2Int + firstResult3_3IntOlim ; 
    	String firstResult3_2StrLast = Integer.toString(firstResult3_2IntLast); 
    	int firstResult3_2IntOlim = 0;
    	if(firstResult3_2StrLast.length() == 2) {
    		firstResult3_2IntOlim = Integer.parseInt(firstResult3_2StrLast.substring(0,1)); 
    		firstResult3_2StrLast = firstResult3_2StrLast.substring(1,firstResult3_2StrLast.length());
    	}
    
    	int firstResult3_1Int = Integer.parseInt(input1_1) *  Integer.parseInt(input2_1);
    	int firstResult3_1IntLast = firstResult3_1Int + firstResult3_2IntOlim ; 
    	String firstResult3_1StrLast = Integer.toString(firstResult3_1IntLast); 
    	
    	String firstResult3 = firstResult3_1StrLast + firstResult3_2StrLast + firstResult3_3StrLast; 
        System.out.println(Integer.parseInt(firstResult3));	    	
    
    	//2단계
    	int plusNum = 2;
    	String[] firstResult1Arr = new String[firstResult3.length()+plusNum];
    	String[] firstResult2Arr = new String[firstResult3.length()+plusNum];
    	String[] firstResult3Arr = new String[firstResult3.length()+plusNum];
    	for(int i=firstResult1Arr.length-1; i>=0; i--) {
    		firstResult1Arr[i] = "0";
    	}
    	int j = firstResult1.length();
    	for(int i=firstResult1Arr.length; i>firstResult1Arr.length-firstResult1.length(); i--) {
    		firstResult1Arr[i-1] = firstResult1.substring(j-1,j);
    		j--;
    	}
    	
    	for(int i=firstResult2Arr.length-1; i>=0; i--) {
    		firstResult2Arr[i] = "0";
    	}
    	j = firstResult2.length();
    	for(int i=firstResult2Arr.length; i>firstResult2Arr.length-firstResult2.length(); i--) {
    		firstResult2Arr[i-2] = firstResult2.substring(j-1,j);
    		j--;
    	}
    	
    	for(int i=firstResult3Arr.length-1; i>=0; i--) {
    		firstResult3Arr[i] = "0";
    	}
        j=firstResult3.length();
    	for(int i=firstResult3Arr.length; i>firstResult3Arr.length-firstResult3.length(); i--) {
    		firstResult3Arr[i-3] = firstResult3.substring(j-1,j);
    		j--;
    	}
    	
        //3단계
    	int[] plusArr = new int[firstResult3Arr.length];
    	int pluArrOlim = 0;
    	for(int i=plusArr.length-1; i>=0; i--) {
    		plusArr[i] = Integer.parseInt(firstResult1Arr[i]) + Integer.parseInt(firstResult2Arr[i]) + Integer.parseInt(firstResult3Arr[i]) +pluArrOlim;
    		pluArrOlim=0;
            //4단계
    		if(plusArr[i] > 9 && i!=0) {
    			pluArrOlim = Integer.parseInt((Integer.toString(plusArr[i])).substring(0,1));
    			plusArr[i] = Integer.parseInt((Integer.toString(plusArr[i])).substring(1,2));
	    	}else {
	    		
	    	}
    	}
    	String result = "";
    	for(int i=0; i<plusArr.length; i++) {
            result +=plusArr[i];
    	}
        //최종결과값
        System.out.println(Integer.parseInt(result));
	}
}

 

 

[다른 해결방법]

 

Scanner 사용

/*
 *  참고사이트
 * https://st-lab.tistory.com/20
 *
 */
import java.util.Scanner;
public class Multiply_2580_reference{
	public static void main(String[] args){
    	Scanner sc = new Scanner(System.in);
    	int input1 = sc.nextInt();
    	int input2 = sc.nextInt();
    	
    	System.out.println(input1 * (input2 % 10)); 		//(3)번결과값
    	System.out.println(input1 * ((input2 % 100) / 10));	//(4)번결과값
    	System.out.println(input1 * (input2 / 100));		//(5)번결과값
    	System.out.println(input1 * input2);				//최종결과값
    	
	}
}

 

BufferedReader 사용

/*
 *  참고사이트
 * https://st-lab.tistory.com/20
 *
 */
package com.baekjoon.algorithm;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Multiply_2580_reference1{
	public static void main(String[] args) throws NumberFormatException, IOException{
    	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    	
    	int input1 = Integer.parseInt(br.readLine());
    	int input2 = Integer.parseInt(br.readLine());
    	
    	br.close();
    	
    	StringBuilder sb = new StringBuilder();
    	
    	sb.append(input1 * (input2 % 10));
    	sb.append('\n');
    	
    	sb.append(input1 * ((input2 % 100)/10));
    	sb.append('\n');
    	
    	sb.append(input1 * (input2 / 100));
    	sb.append('\n');
    	
    	sb.append(input1 * input2);
    	
    	System.out.println(sb);
    	
	}
}

 

 

 

나는 이 문제를 푸는데, 엄청 오랜시간(4시간정도)이 걸렸고, 정말 비효율적인 풀이를 하였는데 구글을 검색하여 보니 정말 간단한 방법이 많았고, 무엇보다 나는 처음 문제의 의도부터 파악을 잘못 하였다는 것을 깨달았다..

그냥 (3),(4),(5)의 결과 값과 최종 결과값만을 출력하면 되는 예제였는데, 일단 처음부터 너무 어렵게만 생각하였던것 같다.. 

BufferedReader는 처음 써보는데, 출력해보니 속도가 굉장히 빨랐다. 

 

 

 

 

출처 :

백준 알고리즘 사이트 www.acmicpc.net/problem/2588

 

2588번: 곱셈

첫째 줄부터 넷째 줄까지 차례대로 (3), (4), (5), (6)에 들어갈 값을 출력한다.

www.acmicpc.net

, st-lab.tistory.com/20

 

[백준] 2588번 : 곱셈 - JAVA [자바]

https://www.acmicpc.net/problem/2588 2588번: 곱셈 첫째 줄부터 넷째 줄까지 차례대로 (3), (4), (5), (6)에 들어갈 값을 출력한다. www.acmicpc.net 문제 매우 간단한 문제다! 3개의 풀이 방법을 제시한다. 이..

st-lab.tistory.com

 

728x90