Page 1 of 6

10033 - Interpreter

Posted: Mon Jul 22, 2002 4:11 pm
by AlexandreN
Anybody has any test cases for this problem ?

10033

Posted: Sat Sep 07, 2002 2:20 pm
by htl
This code always gets WA. Do I misunderstand the meaning of the
statement?
[c]
#include<stdio.h>
void main(void)
{
int count,x,y,now,ans;
long reg[10],ram[1000][3];
char com[5];
scanf("%d\n",&count);
for(x=0;x<count;x++)
{
if(x)
printf("\n");
for(y=0;y<1000;y++)
{
gets(com);
if(!com[0])
break;
ram[y][0]=com[0]-'0',ram[y][1]=com[1]-'0',ram[y][2]=com[2]-'0';
}
for(;y<1000;y++)
ram[y][0]=ram[y][1]=ram[y][2]=0;
for(now=0,ans=0;;)
{
if(ram[now][0]==1)
{
ans++;
break;
}
if(ram[now][0]==2)
reg[ram[now][1]]=ram[now][2];
if(ram[now][0]==3)
{
reg[ram[now][1]]+=ram[now][2];
reg[ram[now][1]]%=1000;
}
if(ram[now][0]==4)
{
reg[ram[now][1]]*=ram[now][2];
reg[ram[now][1]]%=1000;
}
if(ram[now][0]==5)
reg[ram[now][1]]=reg[ram[now][2]];
if(ram[now][0]==6)
{
reg[ram[now][1]]+=reg[ram[now][2]];
reg[ram[now][1]]%=1000;
}
if(ram[now][0]==7)
{
reg[ram[now][1]]*=reg[ram[now][2]];
reg[ram[now][1]]%=1000;
}
if(ram[now][0]==8)
reg[ram[now][1]]=ram[reg[ram[now][2]]][0]*100+ram[reg[ram[now][2]]][0]*10+ram[reg[ram[now][2]]][2];
if(ram[now][0]==9)
ram[reg[ram[now][2]]][0]=reg[ram[now][1]]/100,ram[reg[ram[now][2]]][1]=reg[ram[now][1]]%100/10,ram[reg[ram[now][2]]][2]=reg[ram[now][1]]%10;
if(ram[now][0]==0 && reg[ram[now][2]]!=0)
{
now=reg[ram[now][1]];
ans++;
continue;
}
now++;
ans++;
}
printf("%d\n",ans);
}
}
[/c]

Posted: Mon Sep 09, 2002 8:04 am
by Dominik Michniewski
try to use \[code\] markers, because listing is ... strange ;-))

at fist look code looks nice ....
I use only simulate method and I have only problem with jumps (misunderstanding description...)

Posted: Mon Sep 09, 2002 5:50 pm
by arnsfelt
Just looked at my old code. I don't handle multiple cases, - strange ...

Posted: Tue Jan 28, 2003 1:13 am
by amd-RS
I didn't understand this problem ... should I only count the number of operations ??? And for the sample input the output shouldn't be 15 ???

Thanks, Aur

10033 - Interpretor I'm confused by question...

Posted: Thu Jun 05, 2003 6:20 pm
by PJYelton
I understand how to do the problem I think. What I am confused about is getting the input. There doesn't seem to be any way for the input to let my program know to stop scanning for more commands. I mean, I can exectute commands one at a time, but if one of the commands asks for information about a RAM spot farther ahead or come across a goto statement that tells me to skip ahead, then I need to know what that value is. So instead if I try to scan all the ram at once and THEN execute, then if less than 1000 RAM commands are entered how do I tell my comp to stop scanning and just make the rest zero? The only thing I can think of is if there is a blank line after each case, even the last one, but that isn't stated anywhere. The other idea is that goto statements and queries only ask about previous ram spots, not future ones, but again that isn't stated...

Hopefully someone can understand my question... :-?

Posted: Fri Jun 06, 2003 5:53 am
by cytse
There is a blank line between every two cases, but the last case is followed by an EOF, not a blank line.

Posted: Sat Jun 07, 2003 3:11 am
by rodriwan
Im getting tle in this problem. Maybe Im dealing the 000 case wrong. Can anyone tell me what is wrong with this prog. or what should I do when I read 000...

thanks

[c]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void
deal_in(int op, int *opcode, int *d, int *s)
{
*s = op%10;
op /= 10;
*d = op%10;
op /= 10;
*opcode = op%10;
}

int
main(void)
{
int ram[1005];
int reg[15];
char line[256];
int nins, flag, pos;
int opcode, d, s;
int sum;

fgets(line, 256, stdin);
sscanf(line, "%d", &nins);
fgets(line, 256, stdin);

while (nins--) {
pos = 0;
do {
if (! fgets(line, 256, stdin))
break;
line[strlen(line)-1] = '\0';

ram[pos++] = atoi(line);
} while (strlen(line) > 0);

memset(reg, 0, 15*sizeof(int));
pos = flag = sum = 0;
while (! flag) {
sum++;

deal_in(ram[pos], &opcode, &d, &s);

switch(opcode) {
case 1:
flag = 1;
break;
case 2:
reg[d] = s;
pos++;
break;
case 3:
reg[d] = (reg[d]+s)%1000;
pos++;
break;
case 4:
reg[d] = (reg[d]*s)%1000;
pos++;
break;
case 5:
reg[d] = reg[s];
pos++;
break;
case 6:
reg[d] = (reg[s]+reg[d])%1000;
pos++;
break;
case 7:
reg[d] = (reg[s]*reg[d])%1000;
pos++;
break;
case 8:
reg[d] = ram[reg[s]];
pos++;
break;
case 9:
ram[reg[s]] = reg[d];
pos++;
break;
case 0:
if (reg[s] != 0)
pos = reg[d];
else
pos++;
break;
}
}
printf("%d\n%s", sum, (nins)?"\n":"");
}

return 0;
}
[/c]

Posted: Sat Jun 07, 2003 7:11 pm
by rodriwan
I just forgot to set the ram to 000 before reading. sorry :oops:

istringstream operation causes segfault

Posted: Fri Aug 22, 2003 10:28 am
by symme7ry
When submitting the code below for problem 10033, I get a SIGSEGV runtime error, which I think means a segfault.

[cpp]
#include <iostream>
#include <cstdio>
#include <sstream>
#include <algorithm>
#include <string>

using namespace std;

int main(){
int tests;
string temp;
istringstream iss;
getline(cin, temp);
iss.str(temp);
iss >> tests;
while(1);
return 0;
}
[/cpp]

The line that causes the segfault is "iss >> tests;" which I found out by moving the while(1); statement around. I can't reproduce the error with my local gcc compiler. Does anyone have any idea why I get a segfault here?

Posted: Sat Aug 23, 2003 5:55 pm
by UFP2161
Try this instead:
[cpp]istringstream iss (temp);[/cpp]

Posted: Mon Aug 25, 2003 11:22 pm
by symme7ry
Right, that works, but it is odd that my simple code above breaks their compiler.

Posted: Tue Aug 26, 2003 12:22 am
by UFP2161
I think it's possible their implementation of the istringstream class might have initialized the internal string to NULL since you never passed it one to begin with and when you try to read from NULL, it doesn't work. And somehow the str() didn't change the pointer. It happens on my machine too, but only after the second time I call str().. hence, the first line is parsed correctly, but all the other lines just returned blanks. *shrug*

10033 Interpreter is not Interpreting

Posted: Wed Oct 08, 2003 3:14 pm
by nikhil
10033 Interpreter
i used following code got "WA"


#include <stdio.h>

int ram[1000];
int r[10];
int i,ip,a,b,c;

int main(){
while (1 == scanf("%d",&ram[i++])){}
for (i=1;;i++){
a = ram[ip]/100;
b = ram[ip]/10%10;
c = ram[ip]%10;
ip++;
if (a == 1) break;
if (a == 2) r = c;
else if (a == 3) r = (r+c)%1000;
else if (a == 4) r = (r*c)%1000;
else if (a == 5) r = r[c];
else if (a == 6) r = (r+r[c])%1000;
else if (a == 7) r = (r*r[c])%1000;
else if (a == 8) r[b] = ram[r[c]];
else if (a == 9) ram[r[c]] = r[b];
else if (a == 0 && r[c] !=0) ip = r[b];
}
printf("%d\n",i);
return 0;
}
for advance thanx

Re: 10033

Posted: Wed Oct 15, 2003 12:20 pm
by Algoritmo
Hey, I found a problem on your code. It's the ram[][] variable.
For example, you used ram[][0] when you should have used ram[][1].
You should substitute ram[][] by ram[] and extract digits to a, d and s variables.

your code:
reg[ram[now][1]]=ram[reg[ram[now][2]]][0]*100+ram[reg[ram[now][2]]][0]*10+ram[reg[ram[now][2]]][2];
if(ram[now][0]==9)
ram[reg[ram[now][2]]][0]=reg[ram[now][1]]/100,ram[reg[ram[now][2]]][1]=reg[ram[now][1]]%100/10,ram[reg[ram[now][2]]][2]=reg[ram[now][1]]%10;

htl wrote:This code always gets WA. Do I misunderstand the meaning of the
statement?