문제) 백준 - 구현 - 2차원 배열의 합
https://www.acmicpc.net/problem/2167
2167번: 2차원 배열의 합
첫째 줄에 배열의 크기 N, M(1 ≤ N, M ≤ 300)이 주어진다. 다음 N개의 줄에는 M개의 정수로 배열이 주어진다. 배열에 포함되어 있는 수는 절댓값이 10,000보다 작거나 같은 정수이다. 그 다음 줄에는
www.acmicpc.net
배열에 저장된 수에서 인덱스의 쿼리가 주어질 때 합을 구하는 문제였습니다.
C++ 소스 코드)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
#define endl "\n" | |
#define ll long long | |
#define INF 987654321 | |
#define MAX 301 | |
#define MOD 10007 | |
using namespace std; | |
typedef pair<int, int> p; | |
int N, M; | |
int board[MAX][MAX]; | |
int solve(int a, int b, int c, int d){ | |
int res = 0; | |
for(int i = a; i <= c; ++i) | |
for(int j = b; j <= d; ++j) | |
res += board[i][j]; | |
return res; | |
} | |
int main() { | |
ios_base::sync_with_stdio(0); | |
cin.tie(0); cout.tie(0); | |
cin >> N >> M; | |
for(int i = 0; i < N; ++i) | |
for(int j = 0; j < M; ++j) | |
cin >> board[i][j]; | |
int tc; cin >> tc; | |
while(tc--){ | |
int a, b, c, d; cin >> a >> b>> c>>d; | |
a--; b--; c--; d--; | |
cout << solve(a, b, c, d)<< endl; | |
} | |
return 0; | |
} |
반응형
'PS(Problem Solving) > 백준_BOJ' 카테고리의 다른 글
[백준] 21610번 - 마법사 상어와 비바라기 (C++) 문제 및 풀이 (0) | 2022.02.17 |
---|---|
[백준] 14712번 - 넴모넴모 (Easy) (C++) 문제 및 풀이 (0) | 2022.02.17 |
[백준] 14722번 - 우유 도시 (C++) 문제 및 풀이 (0) | 2022.02.16 |
[백준] 18353번 - 병사 배치하기 (C++) 문제 및 풀이 (0) | 2022.02.16 |
[백준] 2160번 - 그림 비교 (C++) 문제 및 풀이 (0) | 2022.02.16 |
댓글