알고리즘

[알고리즘] 백준 22193 - Multiply (C++)

blueberrysoda 2024. 10. 13. 23:57

[문제 링크] : https://www.acmicpc.net/problem/22193

#include <iostream>
using namespace std;

int N, M;
char A[50000], B[50000];
int Arr[100000];

int main(){
    cin >> N >> M >> A >> B;
    for(int i=0; i<M; i++){
        for(int j=0; j<N; j++){
            int temp = (A[N-1-j]-'0') * (B[M-1-i]-'0');
            Arr[N+M-1-i-j] += temp;  
        }
    }

    for(int i=N+M-1; i>0; i--){
        Arr[i-1] += Arr[i] / 10;
        Arr[i] %= 10; 
    }

    bool flag = false;
    for(int i=0; i<N+M; i++){
        if(flag == false){
            if(Arr[i] != 0){
                flag = true;
            }
            else if(i == N+M-1){
                cout << "0";
            }
        }
        if(flag == true){
            cout << Arr[i];
        }
    }
    cout << "\n";
    return 0;
}