PS/Baek-Joon C++

[백준 C++] 단계별 문제풀이 1단계 < 2588 : 곱셈 >

코딩뚜벅이 2024. 1. 24. 14:50

곱셈

  • 문제
  • 풀이
  • 결과

문제

 


 

풀이

#include <iostream>
#include <cmath>
using namespace std;

int main() 
{
    int nFirst = 0;
    int nSecond = 0;
    int nSecondNumArray[3];
    
    cin >> nFirst;
    cin >> nSecond;
    
    nSecondNumArray[2] = nSecond / 100; 
    nSecondNumArray[1] = (nSecond % 100) / 10; 
    nSecondNumArray[0] = (nSecond % 100) % 10; 
    
    int nTotal = 0;
    
    for(int i=0; i<3; i++) {
        int nResult = 0;
        nResult += nFirst * nSecondNumArray[i];
        cout << nResult << endl;
        nTotal += nResult * pow(10, i);
    }
    cout << nTotal << endl;
}

 

저는 두 번째로 입력 받은 세자릿수 값을 배열로 저장하였습니다. 이후 반복문을 통해 첫 번째로 입력 받은 값과 두 번째 입력 받은 값의 각 자릿수 숫자의 곱셈 연산을 진행하였고, 총 합의 계산에는 제곱 함수인 pow()를 사용했습니다.

 


 

결과