10033 - Interpreter

All about problems in Volume 100. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

AlexandreN
New poster
Posts: 27
Joined: Sun Jul 07, 2002 6:46 pm
Location: Campina Grande - Brazil
Contact:

10033 - Interpreter

Post by AlexandreN »

Anybody has any test cases for this problem ?
htl
Experienced poster
Posts: 185
Joined: Fri Jun 28, 2002 12:05 pm
Location: Taipei, Taiwan

10033

Post 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]
Dominik Michniewski
Guru
Posts: 834
Joined: Wed May 29, 2002 4:11 pm
Location: Wroclaw, Poland
Contact:

Post 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...)
arnsfelt
New poster
Posts: 44
Joined: Wed Oct 17, 2001 2:00 am
Location: Denmark
Contact:

Post by arnsfelt »

Just looked at my old code. I don't handle multiple cases, - strange ...
amd-RS
New poster
Posts: 27
Joined: Thu Sep 05, 2002 7:37 am

Post 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
PJYelton
New poster
Posts: 20
Joined: Fri May 30, 2003 6:56 pm

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

Post 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... :-?
User avatar
cytse
Learning poster
Posts: 67
Joined: Mon Sep 16, 2002 2:47 pm
Location: Hong Kong
Contact:

Post by cytse »

There is a blank line between every two cases, but the last case is followed by an EOF, not a blank line.
rodriwan
New poster
Posts: 8
Joined: Mon Jun 03, 2002 8:13 pm

Post 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]
rodriwan
New poster
Posts: 8
Joined: Mon Jun 03, 2002 8:13 pm

Post by rodriwan »

I just forgot to set the ram to 000 before reading. sorry :oops:
symme7ry
New poster
Posts: 7
Joined: Fri Aug 22, 2003 10:23 am

istringstream operation causes segfault

Post 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?
UFP2161
A great helper
Posts: 277
Joined: Mon Jul 21, 2003 7:49 pm
Contact:

Post by UFP2161 »

Try this instead:
[cpp]istringstream iss (temp);[/cpp]
Last edited by UFP2161 on Tue Aug 26, 2003 12:19 am, edited 1 time in total.
symme7ry
New poster
Posts: 7
Joined: Fri Aug 22, 2003 10:23 am

Post by symme7ry »

Right, that works, but it is odd that my simple code above breaks their compiler.
UFP2161
A great helper
Posts: 277
Joined: Mon Jul 21, 2003 7:49 pm
Contact:

Post 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*
nikhil
New poster
Posts: 11
Joined: Wed Oct 08, 2003 1:37 pm

10033 Interpreter is not Interpreting

Post 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
Algoritmo
New poster
Posts: 32
Joined: Wed Oct 15, 2003 12:10 pm
Contact:

Re: 10033

Post 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?
Post Reply

Return to “Volume 100 (10000-10099)”