기본 콘텐츠로 건너뛰기

2740 : 행렬 곱셈 (Divide and Conquer) [C++]

#include <iostream>
#include <vector>

using namespace std;

void Matrix(vector<vector<int>> A, vector<vector<int>> B)
{
 vector<vector<int>> C;
 int SIZE;

 if (A[0].size() > B[0].size())
  SIZE = A[0].size();
 else
  SIZE = B[0].size();

 for (int i = 0; i < SIZE; i++)
 {
  vector<int> Temp;
  Temp.resize(SIZE);
  C.push_back(Temp);
 }

 int ARow = 0;
 int ACol = 0;
 int BRow = 0;
 int BCol = 0;
 int CRow = 0;
 int CCol = 0;
 int Sum = 0;

 while (true)
 {
  Sum += A[ARow][ACol++] * B[BRow++][BCol];

  if (BRow > A[0].size() - 1)
  {
   if (CCol > C[0].size() - 1)
   {
    CCol = 0;
    CRow++;
   }
   C[CRow][CCol++] = Sum;
   Sum = 0;
   BCol++;
   BRow = 0;
   ACol = 0;
  }
  if (BCol > B[0].size() - 1)
  {
   ACol = 0;
   ARow++;
   BRow = 0;
   BCol = 0;
  }
  if (ARow > B[0].size() - 1)
   break;
 }

 for (int i = 0; i < SIZE; i++)
 {
  for (int j = 0; j < SIZE; j++)
  {
   cout << C[i][j] << " ";
  }
  cout << endl;
 }
}

int main()
{
 int N, M;

 cin >> N >> M;

 vector<vector<int>> A;
 vector<vector<int>> B;

 for (int i = 0; i < N; i++)
 {
  vector<int> Temp;
  Temp.resize(M);
  A.push_back(Temp);
  for (int j = 0; j < M; j++)
  {
   cin >> A[i][j];
  }
 }

 cin >> M >> N;

 for (int i = 0; i < M; i++)
 {
  vector<int> Temp;
  Temp.resize(N);
  B.push_back(Temp);
  for (int j = 0; j < N; j++)
  {
   cin >> B[i][j];
  }
 }

 Matrix(A, B);

 return 0;
}

댓글

이 블로그의 인기 게시물

1978 : 소수 찾기 [C++]

# include < iostream > # include < vector > using namespace std ; int main ( ) { cin . tie ( NULL ) ; vector < int > Primes ; Primes . push_back ( 2 ) ; Primes . push_back ( 3 ) ; for ( int i = 4 ; i < 1000 ; i + + ) { bool IsPrime = true ; if ( i % 2 = = 0 | | i % 3 = = 0 ) continue ; for ( int j = 4 ; j < i ; j + + ) { if ( i % j = = 0 ) { IsPrime = false ; break ; } } if ( IsPrime ) Primes . push_back ( i ) ; } int N , Count = 0 ; cin > > N ; for ( int i = 0 ; i < N ; i + + ) { int Input ; cin > > Input ; for ( int j = 0 ; j < Primes . size ( ) ; j + + ) if ( Input = = Primes [ j ] ) Count + + ; } cout < < Count < < " \n " ; return 0 ; }

10828 : 스택 [Python]

Stack = [ ] def push ( num ) : Stack . append ( int ( num ) ) def pop ( ) : if len ( Stack ) > 0 : print ( Stack . pop ( ) ) else : print ( - 1 ) def size ( ) : print ( len ( Stack ) ) def empty ( ) : if len ( Stack ) == 0 : print ( 1 ) else : print ( 0 ) def top ( ) : if len ( Stack ) > 0 : print ( Stack [ len ( Stack ) - 1 ] ) else : print ( - 1 ) TestCase = int ( input ( ) ) while TestCase > 0 : Command = input ( ) if Command == 'top' : top ( ) elif Command == 'pop' : pop ( ) elif Command == 'empty' : empty ( ) elif Command == 'size' : size ( ) else : push ( Command [ 5 : ] ) TestCase - = 1