이진수로 주어지기 때문에 십진수로 변환하는 작업을 해야했던 문제였습니다.
This file contains hidden or 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 <iostream> | |
#include <vector> | |
using namespace std; | |
int main(void) | |
{ | |
ios_base::sync_with_stdio(0); | |
cin.tie(0); | |
while (1) | |
{ | |
int mem[32] = { 0, }; | |
for(int i=0; i<32; i++) | |
for (int j = 0; j < 8; j++) | |
{ | |
int bit; | |
//EOF | |
if (scanf("%1d", &bit) < 0) | |
return 0; | |
mem[i] = mem[i] * 2 + bit; | |
} | |
int PC = 0; | |
int acc = 0; | |
while (1) | |
{ | |
int instruction = mem[PC]; | |
//상위 3 bit | |
int op = instruction / 32; | |
//하위 5 bit | |
int val = instruction % 32; | |
if (op == 7) | |
break; | |
PC = (PC + 1) % 32; | |
//2^8 = 256 | |
switch (op) | |
{ | |
case 0: | |
mem[val] = acc; | |
break; | |
case 1: | |
acc = mem[val]; | |
break; | |
case 2: | |
if (!acc) | |
PC = val; | |
break; | |
case 4: | |
acc = (acc + 255) % 256; | |
break; | |
case 5: | |
acc = (acc + 1) % 256; | |
break; | |
case 6: | |
PC = val; | |
break; | |
} | |
} | |
//8bit | |
for (int i = 7; i >= 0; i--) | |
printf("%1d", (acc >> i) & 1); | |
printf("\n"); | |
} | |
return 0; | |
} |

개발환경:Visual Studio 2017
지적, 조언, 질문 환영입니다! 댓글 남겨주세요~
반응형
'알고리즘 > BOJ' 카테고리의 다른 글
백준 2239번 스도쿠 (0) | 2019.04.08 |
---|---|
백준 14890번 경사로 (2) | 2019.04.04 |
백준 16551번 Potato Sacks (0) | 2019.03.27 |
백준 4307번 개미 (2) | 2019.03.15 |
백준 10826번 피보나치 수 4 (0) | 2019.03.15 |