C++
이차원 배열에서 패턴찾기 1,9,1
2sac
2024. 10. 11. 14:07
열이 3이 되면 바로 함수를 종료 시키는 메리트가 있음.
2차원 배열도 어차피 1차원으로 저장되어 있는 것 유의
#include <iostream>
#include <cstring>
using namespace std;
int arr[3][5] = {
4,5,1,9,1,
2,2,3,1,9,
1,3,1,9,1
};
int brr[3] = { 1,9,1 };
int isSame(int n, int m) {
if (m == 3) return 0;
for (int x = 0; x < 3; x++) {
if (brr[x] != arr[n][m+x]) return 0;
}
return 1;
}
int main() {
int cnt = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 5; j++) {
if(isSame(i, j)) cnt++;
}
}
cout << "1,9,1 패턴 개수" << cnt;
return 0;
}