10033 - Interpreter
Moderator: Board moderators
-
- New poster
- Posts: 27
- Joined: Sun Jul 07, 2002 6:46 pm
- Location: Campina Grande - Brazil
- Contact:
10033 - Interpreter
Anybody has any test cases for this problem ?
10033
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]
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]
-
- Guru
- Posts: 834
- Joined: Wed May 29, 2002 4:11 pm
- Location: Wroclaw, Poland
- Contact:
10033 - Interpretor I'm confused by question...
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...![:-?](./images/smilies/icon_confused.gif)
Hopefully someone can understand my question...
![:-?](./images/smilies/icon_confused.gif)
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]
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]
istringstream operation causes segfault
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?
[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?
Try this instead:
[cpp]istringstream iss (temp);[/cpp]
[cpp]istringstream iss (temp);[/cpp]
Last edited by UFP2161 on Tue Aug 26, 2003 12:19 am, edited 1 time in total.
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
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 ==
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
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)](./images/smilies/icon_cool.gif)
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
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;
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?