Posted on 2008-04-03 13:41
superman 阅读(260)
评论(0) 编辑 收藏 引用 所属分类:
ZOJ
Implement #1:
1 /* Accepted 1098 C++ 00:00.01 844K */
2 #include <stack>
3 #include <string>
4 #include <iostream>
5
6 using namespace std;
7
8 int main()
9 {
10 string mem[32];
11
12 while(cin >> mem[0])
13 {
14 int accu, pc;
15
16 for(int i = 1; i < 32; i++)
17 cin >> mem[i];
18
19 accu = 0; pc = 0;
20 while(true)
21 {
22 int x = 0, m[] = {16, 8, 4, 2, 1};
23
24 string ins = mem[pc].substr(0, 3);
25 string num = mem[pc].substr(3, 8);
26 for(int i = 0; i < num.size(); i++)
27 x += (num[i] - '0') * m[i];
28
29 pc = (++pc) % 32;
30
31 if(ins == "000")
32 {
33 stack <char> st;
34 int n = accu;
35 while(n)
36 {
37 st.push(n % 2 + '0');
38 n /= 2;
39 }
40 while(st.size() < 8)
41 st.push('0');
42 for(int i = 0; i < 8; i++)
43 {
44 mem[x][i] = st.top();
45 st.pop();
46 }
47 }
48 if(ins == "001")
49 {
50 accu = 0;
51 int m[] = {128, 64, 32, 16, 8, 4, 2, 1};
52 for(int i = 0; i < 8; i++)
53 accu += (mem[x][i] - '0') * m[i];
54 }
55 if(ins == "010")
56 if(accu == 0)
57 pc = x;
58 if(ins == "100")
59 {
60 accu--;
61 if(accu < 0)
62 accu = 255;
63 }
64 if(ins == "101")
65 accu = (++accu) % 256;
66 if(ins == "110")
67 pc = x;
68 if(ins == "111")
69 break;
70 }
71
72 stack <int> st;
73 while(accu)
74 {
75 st.push(accu % 2);
76 accu /= 2;
77 }
78 while(st.size() < 8)
79 st.push(0);
80 for(int i = 0; i < 8; i++)
81 {
82 cout << st.top();
83 st.pop();
84 }
85 cout << endl;
86 }
87
88 return 0;
89 }
90
Implement #2:
1 /* Accepted 1098 C++ 00:00.01 836K */
2 #include <string>
3 #include <iostream>
4
5 using namespace std;
6
7 int main()
8 {
9 string s;
10 while(cin >> s)
11 {
12 int mem[32] = {0}, m[] = {128, 64, 32, 16, 8, 4, 2, 1};
13
14 for(int i = 0; i < 8; i++)
15 mem[0] += (s[i] - '0') * m[i];
16 for(int i = 1; i < 32; i++)
17 {
18 cin >> s;
19 for(int j = 0; j < 8; j++)
20 mem[i] += (s[j] - '0') * m[j];
21 }
22
23 int accu = 0, pc = 0;
24 bool over = false;
25 while(over == false)
26 {
27 pc %= 32;
28
29 int x = mem[pc] & 31; // (31)10 = (00011111)2
30 switch(mem[pc] & 224) // (224)10 = (11100000)2
31 {
32 case 0 : mem[x] = accu; pc++; break;
33 case 32 : accu = mem[x]; pc++; break;
34 case 64 : if(accu == 0)
35 pc = x;
36 else
37 pc++; break;
38 case 96 : pc++; break;
39 case 128 : accu--;
40 if(accu < 0)
41 accu = 255;
42 pc++; break;
43 case 160 : accu = (++accu) % 256;
44 pc++; break;
45 case 192 : pc = x; break;
46 case 224 : over = true; break;
47 }
48 }
49 for(int i = 7; i >= 0; i--)
50 cout << ((accu >> i) & 1);
51 cout << endl;
52 }
53
54 return 0;
55 }
56