[SWEA/C++] 1959 두 개의 숫자열

Hanbi·2022년 5월 30일
0

Problem Solving

목록 보기
23/108
post-thumbnail

문제

https://swexpertacademy.com/main/code/problem/problemDetail.do?problemLevel=2&contestProbId=AV5PpoFaAS4DFAUq&categoryId=AV5PpoFaAS4DFAUq&categoryType=CODE&problemTitle=&orderBy=RECOMMEND_COUNT&selectCodeLang=CCPP&select-1=2&pageSize=10&pageIndex=3

풀이

  • 두 개의 배열 다루기 : 밑에 식을 코드로 구현할 때 해맴
    a[0]b[0] + a[1]b[1] + a[2]b[2]
    a[0]b[1] + a[1]b[2] + a[2]b[3]
    a[0]b[2] + a[1]b[3] + a[2]b[4]
for (int offset = 0; offset < cnt; offset++) {
	for (int i = 0; i < 20; i++) {
		sum += a[i + offset] * b[i];
	}
}
  • sum 변수 초기화

코드

#include <iostream>
#include <vector>

using namespace std;

int main() {
	int test_case;
	int T;

	cin >> T;
	for (test_case = 1; test_case <= T; ++test_case) {
		int N, M;
		int a[21] = { 0 };
		int b[21] = { 0 };

		cin >> N >> M;
		for (int i = 0; i < N; i++) {
			cin >> a[i];
		}
		for (int i = 0; i < M; i++) {
			cin >> b[i];
		}

		int cnt = abs(N - M) + 1;
		int sum = 0, ans = 0;
		
		if (N > M) {
			for (int offset = 0; offset < cnt; offset++) {
				for (int i = 0; i < 20; i++) {
					sum += a[i + offset] * b[i];
				}
				if (sum > ans)	ans = sum;
				sum = 0;
			}
		}
		else {
			for (int offset = 0; offset < cnt; offset++) {
				for (int i = 0; i < 20; i++) {
					sum += a[i] * b[i + offset];
				}
				if (sum > ans)	ans = sum;
				sum = 0;
			}
		}
		
		cout << "#" << test_case << " " << ans << endl;
	}

	return 0;
}
profile
👩🏻‍💻

0개의 댓글