首页 > ACM题库 > HDU-杭电 > Hdu 1341 Simple Computers-模拟[解题报告] C++
2013
12-09

Hdu 1341 Simple Computers-模拟[解题报告] C++

Simple Computers

问题描述 :

You are to write an interpreter for a simple computer. This computer uses a processor with a small number of machine instructions. Furthermore, it is equipped with 32 byte of memory, one 8-bit accumulator (accu) and a 5-bit program counter (pc). The memory contains data as well as code, which is the usual von Neumann architecture.
The program counter holds the address of the instruction to be executed next. Each instruction has a length of 1 byte – the highest 3 bits define the type of instruction and the lowest 5 bits define an optional operand which is always a memory address (xxxxx). For instructions that don’t need an operand the lowest 5 bits have no meaning (—–). Here is a list of the machine instructions and their semantics:000xxxxx STA x store the value of the accu into memory byte x
001xxxxx LDA x load the value of memory byte x into the accu
010xxxxx BEQ x if the value of the accu is 0 load the value x into the pc
011—– NOP no operation
100—– DEC subtract 1 from the accu
101—– INC add 1 to the accu
110xxxxx JMP x load the value x into the pc
111—– HLT terminate program

In the beginning, program counter and accumulator are set to 0. After fetching an instruction but before its execution, the program counter is incremented. You can assume that programs will terminate.

输入:

The input file contains several test cases. Each test case specifies the contents of the memory prior to execution of the program. Byte 0 through 31 are given on separate lines in binary representation. A byte is denoted by its highest-to-lowest bits. Input is terminated by EOF.

输出:

For each test case, output on a line the value of the accumulator on termination in binary representation, again highest bits first.

样例输入:

00111110
10100000
01010000
11100000
00000000
00000000
00000000
00000000
00000000
00000000
00000000
00000000
00000000
00000000
00000000
00000000
00111111
10000000
00000010
11000010
00000000
00000000
00000000
00000000
00000000
00000000
00000000
00000000
00000000
00000000
11111111
10001001

样例输出:

10000111

#include <stdio.h>
#include <string.h>

unsigned char Memory[32];
unsigned char Input[32][9];
unsigned char accu, pc;
unsigned char temp;

int main()
{
    int i, j;
    unsigned char mask;

    while(1){
        for(i = 0; i < 32; i++){
            if(gets(&Input[i][0]) == NULL) return 0;
            Memory[i] = 0;
            for(j = 0; j < 8; j++) Memory[i] = (Memory[i] << 1) + (Input[i][j] - '0');
        }
        pc = accu = 0;
        while(1){
            temp = Memory[pc++];
            if(pc > 0x1F) pc = 0;
            switch(temp >> 5){
            case 0:
                Memory[temp & 0x1F] = accu;
                break;
            case 1:
                accu = Memory[temp & 0x1F];
                break;
            case 2:
                if(accu == 0) pc = temp & 0x1F;
                break;
            case 3:
                break;
            case 4:
                accu--;
                break;
            case 5:
                accu++;
                break;
            case 6:
                pc = temp & 0x1F;
                break;
            case 7:
                break;
            }
            if((temp >> 5) == 7) break;
        }

        mask = 0x80;
        for(i = 0; i < 8; i++){
            putchar((accu & mask) ? '1' : '0');
            mask = mask >> 1;
        }
        putchar('/n');
    }
}

 


  1. #!/usr/bin/env python
    def cou(n):
    arr =
    i = 1
    while(i<n):
    arr.append(arr[i-1]+selfcount(i))
    i+=1
    return arr[n-1]

    def selfcount(n):
    count = 0
    while(n):
    if n%10 == 1:
    count += 1
    n /= 10
    return count

  2. L(X [0 .. M-1],Y [0 .. N-1])= 1 + L(X [0 .. M-2],Y [0 .. N-1])这个地方也也有笔误
    应改为L(X [0 .. M-1],Y [0 .. N-1])= 1 + L(X [0 .. M-2],Y [0 .. N-2])

  3. 学算法中的数据结构学到一定程度会乐此不疲的,比如其中的2-3树,类似的红黑树,我甚至可以自己写个逻辑文件系统结构来。