SW ExpertAcademy/D2
1959. 두 개의 숫자열
Programmer.
2018. 12. 1. 01:20
1959. 두 개의 숫자열
풀이 방법
길이가 더 긴 배열을 고정시키고 짧은 배열을 움직여가면서 곱을 구해 그중에 최대 값을 구한다.
<코드>
#include<iostream> using namespace std; int main(int argc, char** argv) { int test_case; int T; cin >> T; for (test_case = 1; test_case <= T; ++test_case) { int firstSize{}; int secondSize{}; std::cin >> firstSize >> secondSize; int* firstArray = new int[firstSize] {0, }; int* secondArray = new int[secondSize] {0, }; for (int i = 0; i < firstSize; i++) std::cin >> firstArray[i]; for (int i = 0; i < secondSize; i++) std::cin >> secondArray[i]; int* shortArray{ nullptr }; int* longArray{ nullptr }; int shortSize{}; int longSize{}; if (firstSize > secondSize) { shortArray = secondArray; longArray = firstArray; shortSize = secondSize; longSize = firstSize; } else { longArray = secondArray; shortArray = firstArray; shortSize = firstSize; longSize = secondSize; } int max{ 0 }; for (int i = 0; i < longSize - shortSize + 1; i++) { int sum{}; for (int j = 0; j < shortSize; j++) sum += longArray[j] * shortArray[j]; if (sum > max) max = sum; longArray++; } std::cout << "#" << test_case << " " << max << "\n"; delete[] firstArray; delete[] secondArray; } return 0;//정상종료시 반드시 0을 리턴해야합니다. }