Interpreter

From Progteam

Revision as of 01:01, 19 June 2008 by Kerry (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Checkmark.jpg This problem has been solved by mlc413.
Checkmark.jpg This problem has been solved by kerry.


Interpreter
Problem Number 2577
Sorter: kerry
Source: Programming Challenges
Link: http://acm.pku.edu.cn/JudgeOnline/problem?id=2577



Interpreter is problem number 2577 on the Peking University ACM site.

 

import java.util.*;

public class Main{

    public static Scanner in;
    public static int [] register;
    public static int [] ram;
    public static int inst;
    public static boolean done;
    public static int pointer;
 
    public static void main(String[] args){
        in=new Scanner(System.in);

        doStuff();
    }

    public static void doStuff(){
	register = new int[10];
	ram = new int[1000];
	done = false;
	int i = 0;
	while(in.hasNext()){
	    int N=in.nextInt();
	    ram[i]= N;
	    // System.out.println(ram[i]);
	    i++;
	}
	
	pointer = 0;
	while(!done){
	    getCmd(ram[pointer]);
	    pointer++;
	}
	

	System.out.println(inst);
    }

    public static void getCmd(int cmd){
	inst++;
	int one = cmd/100;
	switch(one){
	    case 0:
		if(register[cmd%10]  != 0){
		    pointer = register[cmd/10%10];
		    getCmd(ram[pointer]);
		}
	        break;
	    case 1:
		done = true;
	        break;
	    case 2:
		register[cmd/10%10] = cmd%10;
		register[cmd/10%10] %= 1000;
	        break;
	    case 3:
		register[cmd/10%10] += cmd%10;
		register[cmd/10%10] %= 1000;
	        break;
	    case 4:
		register[cmd/10%10] *= cmd%10;
		register[cmd/10%10] %= 1000;
	        break;
	    case 5:
		register[cmd/10%10] = register[cmd%10];
		register[cmd/10%10] %= 1000;
	        break;
	    case 6:
		register[cmd/10%10] += register[cmd%10];
		register[cmd/10%10] %= 1000;
	        break;
	    case 7:
		register[cmd/10%10] *= register[cmd%10];
		register[cmd/10%10] %= 1000;
	        break;
	    case 8:
		register[cmd/10%10] = ram[register[cmd%10]];
		register[cmd/10%10] %= 1000;
	        break;
	    case 9:
		ram[register[cmd%10]] = register[cmd/10%10];
		register[cmd%10] %= 1000;
	        break;
	    
	}
    }
}



Kerry's C answer

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

int act(int[1000][3], int[10],int*, int*);
int get_input(int[1000][3]);

int main() {
int ram[1000][3], i, j, num, registers[10], go, count;

                count=0;
                for(i=0;i<10;i++)
                        registers[i]=0;

                for(i=0;i<1000;i++) {
                        ram[i][0]=0;
                        ram[i][1]=0;
                        ram[i][2]=0;
                }
                get_input(ram);
                i=0;
                go=0;
                while(1) {
                        go=act(ram, registers, &i, &count);
                        if (go) break;
                }
                printf("%d\n", count);
}

int get_input(int ram[1000][3]) {
int i, j;
char a,b;
        i = j =0;
        a=getchar();
        b=0;
        while(a!=EOF && b!=EOF) {
                ram[i][j++]=(a-'0');
                ram[i][j++]=(getchar()-'0');
                ram[i++][j]=(getchar()-'0');
                j=0;
                b=getchar();
                a=getchar();
        }
}

int act(int ram[1000][3], int registers[10], int *inst, int *count) {
        *count=*count+1;
        switch(ram[*inst][0]) {
                case 0:
                        if (registers[ram[*inst][2]]!=0)
                                *inst=registers[ram[*inst][1]]-1;
                        break;
                case 1:
                        return(1);
                case 2:
                        registers[ram[*inst][1]]=ram[*inst][2];
                        break;
                case 3:
                        registers[ram[*inst][1]]=((registers[ram[*inst][1]]+ram[*inst][2])%1000);
                        break;
                case 4:
                        registers[ram[*inst][1]]=((registers[ram[*inst][1]]*ram[*inst][2])%1000);
                        break;
                case 5:
                        registers[ram[*inst][1]]=registers[ram[*inst][2]];
                        break;
                case 6:
                        registers[ram[*inst][1]]=((registers[ram[*inst][1]]+registers[ram[*inst][2]])%1000);
                        break;
                case 7:
                        registers[ram[*inst][1]]=((registers[ram[*inst][1]]*registers[ram[*inst][2]])%1000);
                        break;
                case 8:
                        registers[ram[*inst][1]]=ram[registers[ram[*inst][2]]][0]*100+ram[registers[ram[*inst][2]]][1]*10+ram[registers[ram[*inst][2]]][2];
                        break;
                case 9:
                        ram[registers[ram[*inst][2]]][0]=floor(registers[ram[*inst][1]]/100);
                        ram[registers[ram[*inst][2]]][1]=floor((registers[ram[*inst][1]]%100)/10);
                        ram[registers[ram[*inst][2]]][2]=((registers[ram[*inst][1]]%100)%10);
                        break;
        }
        *inst=*inst+1;
        return(0);
}

Personal tools