C++

[C++] 6월 23일 코딩 테스트 수업

k-codestudy 2025. 6. 23. 17:35

프로그래머스 Lv.4

행렬과 연산 (마무리)

#pragma once
#include <stdio.h>
#include <list>

class C_MAT
{
private:
	int**	m_ppNode;
	int		m_nColumn;
	int		m_nRow;
	std::list<int*> m_listRotate;
	std::list<int*>* m_listShift;

	void rotateList(std::list<int*> &list);

public:
	C_MAT() = default;
	void init(int nRow, int nColumn);
	void rotate();
	void print();
	void shift();
};

 

#include "mat.h"

void C_MAT::init(int nRow, int nColumn)
{
	m_nColumn = nColumn;
	m_nRow = nRow;

	int nData = 1;
	m_ppNode = new int* [nRow] {};
	for (int i = 0; i < nRow; i++)
	{
		m_ppNode[i] = new int[m_nColumn] {};
		for (int j = 0; j < m_nColumn; j++)
		{
			m_ppNode[i][j] = nData;
			nData++;
		}
	}

	int arCount[4][3] = 
	{
		{nColumn - 1,0,1}, 
		{nRow - 1,1,0},
		{nColumn - 1,0,-1},
		{nRow - 1,-1,0},
	};
	
	int nY{};
	int nX{};
	for (int i = 0; i < 4; i++)
	{
		for (int j = 0; j < arCount[i][0]; j++)
		{
			m_listRotate.push_front(&m_ppNode[nY][nX]);
			nY += arCount[i][1];
			nX += arCount[i][2];
		}
	}

	m_listShift = new std::list<int*> [m_nColumn] {};

	for (int i = 0; i < m_nColumn; i++)
	{
		for (int j = 0; j < m_nRow; j++)
		{
			m_listShift[i].push_front(&m_ppNode[i][j]);
		}
	}
}

void C_MAT::rotate()
{
	rotateList(m_listRotate);
}

void C_MAT::rotateList(std::list<int*>& list)
{
	std::list<int*>::iterator iterDst = list.begin();
	std::list<int*>::iterator iterSrc = list.begin();
	iterSrc++;

	int nData = **iterDst;

	while (iterSrc != list.end())
	{
		**iterDst = **iterSrc;
		iterDst = iterSrc;
		iterSrc++;
	}
	**iterDst = nData;
}

void C_MAT::print()
{
	for (int i = 0; i < m_nRow; i++)
	{
		for (int j = 0; j < m_nColumn; j++)
		{
			printf("%2d ", m_ppNode[i][j]);
		}
		printf("\n");
	}
	printf("\n");
}

void C_MAT::shift()
{
	for (int i = 0; i < m_nColumn; i++)
	{
		rotateList(m_listShift[i]);
	}
}
#include <iostream>
#include "mat.h"

int main()
{
	C_MAT cMat{};

	cMat.init(3, 3);
	cMat.print();
	//cMat.rotate();
	//cMat.print();

	cMat.shift();
	cMat.print();

	cMat.shift();
	cMat.print();
}

 

 

프로그래머스 LV.2

완전범죄 ( 나머지는 알아서 풀어볼 것 )

#pragma once

#include <stdio.h>
#include <queue>

class C_THIFE
{
private:
	int m_table[4][4];
private:
	struct S_COODMATE
	{
		int n;
		int m;
	};
	struct S_DATA
	{
		int n;
		int m;

		bool operator()(S_DATA& nD, S_DATA& nS)
		{
			if ((nD.n - nD.m) < (nS.n - nS.m))
				return true;
			else if ((nD.n - nD.m) == (nS.n - nS.m))
				return nD.m > nS.m;

			return false;
		}
	};

private:
	std::priority_queue<S_DATA, std::vector<S_DATA>, S_DATA> m_queue;

public:
	C_THIFE() = default;
	void add(int n, int m);
	void print();
};
#include "thief.h"

void C_THIFE::add(int n, int m)
{
	m_queue.push({ n,m });
	m_table[n][m]++;
}

void C_THIFE::print()
{
	while (!m_queue.empty())
	{
		S_DATA sData = m_queue.top();
		printf("n : %d m : %d \n", sData.n, sData.m);
		m_queue.pop();
	} 

	S_COODMATE ar[9]{ {3,1},{2,1},{3,2},{1,1},{2,2},{3,3},{1,2},{2,3},{1,3} };

	for (int i = 0; i < 9; i++)
	{
		printf("%d, %d, %d \n", ar[i].n, ar[i].m, m_table[ar[i].n][ar[i].m]);
	}

}
#include <iostream>
#include "thief.h"

int main()
{
    C_THIFE cThife{};


    cThife.add(1,2);
    cThife.add(2,3);
    cThife.add(2,1);

    cThife.print();
}