*이 글은 2022년 1월~2월 노션으로 진행한 알고리즘 스터디를 옮긴 글입니다. 나동빈 저자 이것이 취업을 위한 코딩테스트다를 사용해 학습했습니다.
<구현에서 다루는 내용>
<구현 시 고려해야 할 메모리 제약사항>
정수형 종류 | 자료형의 크기 | 자료형의 범위 |
---|---|---|
int | 4byte | -2,147,483,648~2,147,438,647 |
long long | 8byte | -9,223,372,036,854,775,808~9,223,372,036,854,775,807 |
biginteger(class) | 가변 | no |
#include<iostream>
using namespace std;
bool datas[100][100] = {};
class cursor {
public:
int x;
int y;
cursor() {
x = 0;
y = 0;
}
};
void r(cursor *c) {
if (datas[c->x][c->y + 1]==true) {
c->y += 1;
}
}
void l(cursor* c) {
if (datas[c->x][c->y -1 ] == true) {
c->y -= 1;
}
}
void u(cursor* c) {
if (datas[c->x-1][c->y] == true) {
c->x -= 1;
}
}
void d(cursor* c) {
if (datas[c->x+1][c->y ] == true) {
c->x += 1;
}
}
int main() {
int n;
string x;
cin >> n >> x;
cursor* c = new cursor();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
datas[i][j] = true;
}
}
for (int i = 0; i < x.size(); i++) {
switch (x[i]) {
case 'R': r(c); break;
case 'L': l(c); break;
case 'U': u(c); break;
case 'D': d(c); break;
}
}
cout << c->x + 1 << " " << c->y + 1;
return 0;
}
#include<iostream>
using namespace std;
int main() {
int n,answer=0;
cin >> n;
for (int i = 0; i <= n; i++) {
if (i == 3 || i == 13) {
answer += 60 * 60;
}
else {
answer += (15 * 60 + 45 * 15);
}
}
cout << answer;
return 0;
}
#include<iostream>
using namespace std;
int main() {
int x, y, answer=0;
string s;
cin >> s;
x = s[0] - 96;
y = s[1];
if (x - 2 >= 1) {
if (y - 1 >= 1)answer += 1;
if (y + 1 <= 8)answer += 1;
}
if (x + 2 <= 8) {
if (y - 1 >= 1)answer += 1;
if (y + 1 <= 8)answer += 1;
}
if (y - 2 >= 1) {
if (x - 1 >= 1)answer += 1;
if (x + 1 <= 8)answer += 1;
}
if (y + 2 <= 8) {
if (x - 1 >= 1)answer += 1;
if (x + 1 <= 8)answer += 1;
}
cout << answer;
return 0;
}
#include<iostream>
using namespace std;
int main() {
int x, y, answer=0;
string s;
cin >> s;
x = s[0] - 96;
**y = s[1] - 48;**
cout<<"문자열 처리 x:" << x << " y:" << y << endl;
if (x - 2 >= 1) {
if (y - 1 >= 1)answer += 1;
if (y + 1 <= 8)answer += 1;
}
if (x + 2 <= 8) {
if (y - 1 >= 1)answer += 1;
if (y + 1 <= 8)answer += 1;
}
if (y - 2 >= 1) {
if (x - 1 >= 1)answer += 1;
if (x + 1 <= 8)answer += 1;
}
if (y + 2 <= 8) {
if (x - 1 >= 1)answer += 1;
if (x + 1 <= 8)answer += 1;
}
cout << answer;
return 0;
}
문자열을 숫자로 처리할 때 알파벳의 경우 잘 처리했는데 숫자는 문자열을 그대로 int형 변수에 갖다 씀. 그 결과 숫자의 아스키코드가 변수에 저장되었고 내가 짠 로직이 수행되지 않음.
알파벳을 처리할 때와 마찬가지로 0의 아스키코드 48을 빼줘서 문자열을 처리했음. a/A아스키코드는 외우고 있었는데 이제 0도 외워야할 듯 함... 다른 방법이 있을까?
서치해보니 sstream stl 사용하여 변환하는 방법이 나옴. 근데 이거 내 코드에 적용해보니까 쓰레기값나옴 -_-...
#include <iostream>
**#include <sstream>**
int main()
{
int i = 0;
std::stringstream ssInt("123");
ssInt >> i;
if (!ssInt.fail())
std::cout << ssInt.str() << std::endl;
return 0;
}
#include <iostream>
using namespace std;
int map[50][50];
int dx[4] = { -1, 0, +1, 0 };
int dy[4] = { 0, -1, 0, +1 };
int main(void) {
int move = 1;
int m, n, x, y, d;
cin >> m >> n;
cin >> x >> y >> d;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> map[i][j];
}
}
while (1) {
map[x][y] = 1;
for (int i = 1; i <= 4; i++) {//4개 방향 회전
int nd = (d + i) % 4; //탐색할 방향
//탐색할 위치
int nx = x + dx[nd];
int ny = y + dy[nd];
if (nx >= 0 && ny >= 0 && nx < n && ny < m && map[nx][ny] == 0) { //전진 가능하다면 위치와 방향 업데이트
d = nd;
x = nx; y = ny;
move++;
break; //for문에 대한 break
}
if (i == 4) { // 모든 면이 바다라면
d = nd; //바라보는 방향 유지
nd = (nd + 2) % 4; //뒤로 갈 때의 방향 얻기
nx = x + dx[nd];
ny = y + dy[nd];
if (nx >= 0 && ny >= 0 && nx < n && ny < m && map[nx][ny] == 1) { //뒷방향도 바다라면
cout << move;
return 0;
}
else { //뒷 방향 갈 수 있다면
x = nx; y = ny;
move++;
}
}
}
}
}