Wierd Problem

Pages: 12
It stops the infinite looping and the program gives the correct output for the sample program, but the judge still says it gives the wrong answer.

EDIT: @keskiverto Thank you
Last edited on
Problem Solved. After scrapping everything I had, I created a solution that was accepted by the judge:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int reg[10], ram[1000];

inline void decode(int ins, int &ir) {
	int opcode=ins/100;
	int op1=(ins%100)/10;
	int op2=ins%10;
	switch(opcode) {
		case 0:
			ir=(reg[op2]!=0 ? reg[op1]-1 : ir);
			break;
		case 1:
			ir=-2;
			break;
		case 2:
			reg[op1]=op2;
			break;
		case 3:
			reg[op1]+=op2;
			reg[op1]%=1000;
			break;
		case 4:
			reg[op1]*=op2;
			reg[op1]%=1000;
			break;
		case 5:
			reg[op1]=reg[op2];
			break;
		case 6:
			reg[op1]+=reg[op2];
			reg[op1]%=1000;
			break;
		case 7:
			reg[op1]*=reg[op2];
			reg[op1]%=1000;
			break;
		case 8:
			reg[op1]=ram[reg[op2]];
			break;
		case 9:
			ram[reg[op2]]=reg[op1];
			break;
		default:
			break;
	}
}
int main() {
	int n;
	string s;
	cin>>n;
	getline(cin,s);
	getline(cin,s);
	for(int j=0; j<n; ++j) {
		int count=0, ir=0;
		for(int i=0; i<10; ++i) reg[i]=0;
		for(int i=0; i<1000; ++i) ram[i]=0;
	
		for(int i=0; i<1000;++i) {
			getline(cin,s);
			if(s=="") break;
			istringstream iss(s);
			iss>>ram[i];
		}
		while(ir>=0) {
			decode(ram[ir], ir);
			++ir;
			++count;
		}
		if(j>0) cout<<'\n';
		cout<<count<<'\n';
	}
}
Good. For fun, compare:
1
2
for ( int i=0; i<1000; ++i ) ram[i]=0;
std::fill( ram, ram+1000, 0 );
I know about the fill operation, I do not use it because I do not know how efficient it is, and considering my program is being timed efficiency is of utmost importance.
Always good to trust your own ad hoc solutions over those that get used by everyone and have been optimized to death. Especially without timing them.
Topic archived. No new replies allowed.
Pages: 12