POJ 1524 - Cowculations

http://poj.org/problem?id=1524

概要

長々と書いてあるけど,ようは

の四進数で表された符号無し16ビット整数で,

の演算を行う.

解法

やるだけ.

 1 #include <iostream>
 2 using namespace std;
 3 
 4 unsigned read(const string& s)
 5 {
 6   unsigned r = 0;
 7   for (string::const_iterator it = s.begin(); it != s.end(); ++it) {
 8     r <<= 2;
 9     switch (*it) {
10       case 'V': r |= 0; break;
11       case 'U': r |= 1; break;
12       case 'C': r |= 2; break;
13       case 'D': r |= 3; break;
14       default: throw "unknown number";
15     }
16   }
17   return r;
18 }
19 
20 int main()
21 {
22   int N;
23   cin >> N;
24   cout << "COWCULATIONS OUTPUT" << endl;
25   while (N-- > 0) {
26     string s;
27     cin >> s;
28     const unsigned x = read(s);
29     cin >> s;
30     unsigned y = read(s);
31     for (int i = 0; i < 3; i++) {
32       cin >> s;
33       switch (s[0]) {
34         case 'A': y += x; break;
35         case 'L': y <<= 2; break;
36         case 'R': y >>= 2; break;
37         case 'N': break;
38         default: throw "unknown operation";
39       }
40       y = y & ((1<<16)-1);
41     }
42     cin >> s;
43     cout << (y == read(s) ? "YES" : "NO") << endl;
44   }
45   cout << "END OF OUTPUT" << endl;
46   return 0;
47 }
poj/1524.cc