Page 6 of 6

Re: 10033 - Interpreter

Posted: Sun Aug 02, 2009 11:37 pm
by panteluke
OK, I have solved the problem. Basically I have used the same methods and just rearranged the input handling/
I still can't find what's wrong with this solution.

Re: 10033 - Interpreter

Posted: Wed Sep 09, 2009 8:41 pm
by barqawi
hey every body here ... hope you help me here

I found the following input and output here and i have 3 questions:
input:

Code: Select all

221
422
425
000
311
712
712
913
031
output:

Code: Select all

10
the 3 questions:
1- the command (913) what is the value of R1???
>>> my sol: go to add in r3 that means 0 in zero there value 1 that is 1 BUT R1 must to be zero to stop at the next line so that output will be 10

2- what i must to do when 000 and I must to increase the output++ or not ?
>>>> my sol: do not do any thing and do not increase the output

3- there is no 100, I must to halt after input finish but i must to increase the out put for halt or just when if there is 100 ?
i.e. what i must to do if there is no 100 ?

please hope you help me

Re: 10033 - Interpreter

Posted: Fri Sep 11, 2009 5:48 pm
by barqawi
hey i knowed the answer for the question 1 ,2,3

case 9 will generate 100 in the input

100 always will be there if not will generated

000 make the output++ ... dont do any comand

the inputs here give me correct answer but when i subment it is give me WA WA WA ... i think some thing with getting the output not proceses

can somebody help me here

Code: Select all



import java.util.Scanner;
import java.util.Vector;


/**
 *
 * @author bjaguar
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        boolean init = true;
        int r[];
        Vector<Integer> ram;
        int noc,inst=0;
        String line;

        noc = Integer.parseInt(sc.nextLine());
        sc.nextLine();

        while (noc !=0){

            // prepare for the test case
            ram = new Vector<Integer>();
            r = new int[10];            

            
            //loop on each test case            
            line = sc.nextLine();            
            while (!line.equals("")){                
                ram.add(Integer.parseInt(line));                
                line = sc.nextLine();
            }//loop on every test case
            noc--;

            
            inst = proces(r, ram);

            System.out.println(inst);
            if (noc != 0)
                System.out.println();
        }//loop on all cases
    }//main

    private static int proces(int r[], Vector<Integer> ram){

        int add = 0;
        int inst = 0;
        boolean stop = false;
        int number, cmd, num1, num2;

       
        
        for (int i=0;i<ram.size() && !stop;i++){
            number = ram.get(i);
            cmd = number/100;
            num1 = number%100/10;
            num2 = number%10;            

            switch(cmd){

                case 2:
                    r[num1] = num2;                                        
                    inst++;
                    break;


                case 3:
                    r[num1] = (r[num1]+num2)%1000;                                        
                    inst++;
                    break;


                case 4:
                    r[num1] = (r[num1]*num2)%1000;                    
                    inst++;
                    break;

                case 5:
                    r[num1] = r[num2];
                    inst++;
                    break;

                case 6:                    
                    r[num1] = (r[num1]+r[num2])%1000;
                    inst++;
                    break;

                case 7:
                    r[num1] = (r[num1]*r[num2])%1000;
                    inst++;
                    break;

                case 8:
                    add = r[num2];
                    r[num1] = ram.get(add);
                    inst++;
                    break;

                case 9:
                    add = r[num2];
                    ram.remove(add);
                    ram.add(add, r[num1]);                     
                    inst++;
                    break;

                case 1:
                    if (num1 == 0 && num2 == 0){
                        stop = true;
                        inst++;
                    }// comand to stop
                    break;

                case 0:
                    if ( r[num2]!=0 && (num1 != 0 || num2 != 0)){                        
                        i = r[num1]-1;                        
                    }// comand to stop
                    inst++;
                    break;
        }//switch on comand type
        }//loop on all comands in the case

        return inst;
    }// proces each case


}//class


Re: 10033 - Interpreter

Posted: Sun Dec 20, 2009 12:03 am
by Ivan Goroun
ExUCI wrote:There is an extra end-of-line at the end of the output, that's all!!! :D ....the empty lines are between cases

Remove your code after AC
You are right, but this is BS. Problem description doesn't state how many new lines should be at the end of the last input. So, they should accept all the reasonable options or state what the heck they want.

I got 2 WA's from having: 2 new lines, and 0 new lines. AC with 1 new line. :roll:

Re: 10033 - Interpreter

Posted: Sun Dec 20, 2009 12:14 am
by Ivan Goroun
I've compiled the various inputs from this thread into one.

Code: Select all

5

299
492
495
399
492
495
399
283
279
689
078
100
000
000
000

221
422
425
000
311
712
712
913
031

299
233
255
990
803
301
050
100

212
415
521
721
532
439
543
631
339
923
029
873
674
027
023

299
492
495
399
492
495
399
589
279
689
279
078
100
000
000
000
The output is EXACTLY as below

Code: Select all

16

10

26

835

3007
There is a SINGLE NEWLINE after 3007 and TWO NEWLINES between outputs.

This tests pretty much everything.

Re: 10033 - Interpreter

Posted: Thu Mar 17, 2011 2:17 pm
by leonardoferrari
Getting Runtime Error, also, I think my code has something wrong, because on the UVA example the output is 16, and Im getting 13 !

My code:

Code: Select all

/* 
 * File:   interpreter.cpp
 * Author: Leonardo
 *
 * Created on 14 de Março de 2011, 02:56
 */

#include <cstdlib>
#include <iostream>
#include <string.h>
#include <stdlib.h>

using namespace std;

int numIns = 0;
int reg[10];
int ram[1001];

void verifica(int ins, int valor) {

int temp, nums[2], pos;

    temp = valor / 10;
    nums[0] = temp;
    temp *= 10;
    valor -= temp;
    nums[1] = valor;

    switch (ins) {
        case 0:
            if(reg[nums[1]] != 0){
                pos = reg[nums[0]];
                numIns += reg[nums[1]];
			}else{
				numIns++;
			}
            break;
        case 1:
            numIns++;
            break;
        case 2:
            reg[nums[0]] = nums[1];
            numIns++;
            break;
        case 3:
            reg[nums[0]] = (reg[nums[0]] + nums[1]) % 1000;
            numIns++;
            break;
        case 4:
            reg[nums[0]] = (reg[nums[0]] * nums[1]) % 1000;
            numIns++;
            break;
        case 5:
            reg[nums[0]] = reg[nums[1]];
            numIns++;
            break;
        case 6:
            reg[nums[0]] = (reg[nums[0]] + reg[nums[1]]) % 1000;
            numIns++;
            break;
        case 7:
            reg[nums[0]] = (reg[nums[0]] * reg[nums[1]]);
            numIns++;
        case 8:
            reg[nums[0]] = ram[reg[nums[1]]];
            numIns++;
            break;
        case 9:
            ram[reg[nums[1]]] = reg[nums[0]];
            numIns++;
            break;
    }
}

int main(int argc, char** argv) {

    int entradas;
    int modulo, instr;

    cin >> entradas;
    cout << endl;

    for (int j = 0; j < entradas; j++) {
        memset(reg, 0, sizeof (reg));
        memset(ram, 0, sizeof (ram));

       do {
            cin >> ram[numIns];
            modulo = ram[numIns] % 100;
            instr = (ram[numIns] - modulo) / 100;
            verifica(instr, modulo);
        } while (instr != 1);
        cout << endl << numIns << endl << endl;

    }
    return 0;
}

Looking forward for an answer !

Thanks !

Re: 10033 - Interpreter

Posted: Sat Mar 01, 2014 5:22 pm
by brianfry713
1 There is one case below

RAM location, RAM value, description
000, 299, Set register 9 to 9 // R9=9
001, 492, Multiply register 9 by 2 // R9=18
002, 495, Multiply register 9 by 5 // R9=90
003, 399, Add 9 to register 9 // R9=99
004, 492, Multiply register 9 by 2 // R9=198
005, 495, Multiply register 9 by 5 // R9=990
006, 399, Add 9 to register 9 // R9=999
007, 283, Set register 8 to 3 // R8=3
008, 279, Set register 7 to 9 // R7=9
009, 689, Add register 9 value (R9=999) to register 8 // R8=2 (b/c mod 1000 happens)
010, 078, Go to location in register 7 (R7=9) unless R8==0 (R8=2)
009, 689, Add register 9 value (R9=999) to register 8 // R8=1 (b/c mod 1000 happens)
010, 078, Go to location in register 7 (R7=9) unless R8==0 (R8=1)
009, 689, Add register 9 value (R9=999) to register 8 // R8=0 (b/c mod 1000 happens)
010, 078, Go to location in register 7 unless R8==0 (R8=0)
011, 100, Halt

Re: 10033 - Interpreter

Posted: Mon Mar 03, 2014 7:32 am
by uDebug
brianfry713 wrote:1 There is one case below
Thanks a bunch for this careful explanation. It's really appreciated. I worked this out again and it makes sense. I do have one question, though.

I don't fully understand how it was clear to you to repeat only the line before the goto until the condition in the goto was met. In other words, how did you know that these couple lines were to be repeated?

Code: Select all

689 Add register 9 value to register 8 
078 Go to location in register 7 unless R8==0
That's not made clear in the problem statement - right?

Re: 10033 - Interpreter

Posted: Thu Sep 04, 2014 3:36 pm
by fresher96
hey please can you explain where the "16" in the output came from ! :roll:

Re: 10033 - Interpreter

Posted: Thu Sep 04, 2014 7:30 pm
by brianfry713
vinit R7 = 9 so you go to that location.

fresher96, read my post in this thread two before yours: by brianfry713 » Sat Mar 01, 2014 7:22 am

Re: 10033 - Interpreter

Posted: Sat Sep 06, 2014 2:08 pm
by fresher96
I've compiled the various inputs from this thread into one
can anyone explain the fourth case of input .... that it's answer is 835
especially for 923 & 023 !
what does 9ds in general means "the value in RAM !!!"

Re: 10033 - Interpreter

Posted: Mon Sep 08, 2014 11:30 pm
by brianfry713

Code: Select all

1: RAM[0] = 212, set register 1 to 2
2: RAM[1] = 415, multiply register 1 = 2 by 5, now = 10
3: RAM[2] = 521, set register 2 to the value of register 1 = 10
4: RAM[3] = 721, multiply register 2 = 10 by the value of register 1 = 10, now = 100
5: RAM[4] = 532, set register 3 to the value of register 2 = 100
6: RAM[5] = 439, multiply register 3 = 100 by 9, now = 900
7: RAM[6] = 543, set register 4 to the value of register 3 = 900
8: RAM[7] = 631, add the value of register 1 = 10 to register 3 = 900, now = 910
9: RAM[8] = 339, add 9 to register 3 = 910, now = 919
10: RAM[9] = 923, set the value in RAM whose address is in register 3 = 919 to the value of register 2 = 100
11: RAM[10] = 29, goto the location in register 2 = 100 unless register 9 = 0 contains 0
12: RAM[11] = 873, set register 7 to the value in RAM whose address is in register 0 = 3, now 919
13: RAM[12] = 674, add the value of register 4 = 900 to register 7 = 100, now = 0
14: RAM[13] = 27, goto the location in register 2 = 100 unless register 7 = 0 contains 0
15: RAM[14] = 23, goto the location in register 2 = 100 unless register 3 = 919 contains 0
16-834: RAM[100-918] = 0, goto the location in register 0 = 0 unless register 0 = 0 contains 0
835: RAM[919] = 100, Halt
835

Re: 10033 - Interpreter

Posted: Tue Sep 09, 2014 2:03 pm
by fresher96
brianfry713 wrote:

Code: Select all

1: RAM[0] = 212, set register 1 to 2
2: RAM[1] = 415, multiply register 1 = 2 by 5, now = 10
3: RAM[2] = 521, set register 2 to the value of register 1 = 10
4: RAM[3] = 721, multiply register 2 = 10 by the value of register 1 = 10, now = 100
5: RAM[4] = 532, set register 3 to the value of register 2 = 100
6: RAM[5] = 439, multiply register 3 = 100 by 9, now = 900
7: RAM[6] = 543, set register 4 to the value of register 3 = 900
8: RAM[7] = 631, add the value of register 1 = 10 to register 3 = 900, now = 910
9: RAM[8] = 339, add 9 to register 3 = 910, now = 919
10: RAM[9] = 923, set the value in RAM whose address is in register 3 = 919 to the value of register 2 = 100
11: RAM[10] = 29, goto the location in register 2 = 100 unless register 9 = 0 contains 0
12: RAM[11] = 873, set register 7 to the value in RAM whose address is in register 0 = 3, now 919
13: RAM[12] = 674, add the value of register 4 = 900 to register 7 = 100, now = 0
14: RAM[13] = 27, goto the location in register 2 = 100 unless register 7 = 0 contains 0
15: RAM[14] = 23, goto the location in register 2 = 100 unless register 3 = 919 contains 0
16-834: RAM[100-918] = 0, goto the location in register 0 = 0 unless register 0 = 0 contains 0
835: RAM[919] = 100, Halt
835
thanks a lot MR.
brianfry713
this was ambiguous for me
and thanks for the detailed description

Re: 10033 - Interpreter

Posted: Sat Nov 08, 2014 1:20 am
by pakosh
Hi, there is my code

Code: Select all

else if (instruction == 9)
				ram[ram[b]] = r[a]; // facepalm
I tried this input

Code: Select all

5

299
492
495
399
492
495
399
283
279
689
078
100
000
000
000

221
422
425
000
311
712
712
913
031

299
233
255
990
803
301
050
100

212
415
521
721
532
439
543
631
339
923
029
873
674
027
023

299
492
495
399
492
495
399
589
279
689
279
078
100
000
000
000
But Im getting this output

Code: Select all

16

10

26

636

3007
instead of

Code: Select all

16

10

26

835

3007
Any suggestions what can be wrong?

EDIT: AC