MiinaMagdy

11956 - Brainfuck

Sep 3rd, 2022
1,172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.07 KB | None | 0 0
  1. /*
  2.  *
  3.  * problem link: https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3107
  4.  *  
  5.  * */
  6.  
  7. #include <bits/stdc++.h>
  8.  
  9. using namespace std;
  10.  
  11. #define ll long long
  12. #define endl '\n'
  13. #define sz(x) int(x.size())
  14. #define all(x) x.begin(), x.end()
  15.  
  16. string hexa(int x) {
  17.     string s;
  18.     while (x > 0) {
  19.         s += (x % 16 > 9 ? 'A' + (x % 16 - 10) : '0' + x % 16);
  20.         x /= 16;
  21.     }
  22.     while (sz(s) < 2) s += '0';
  23.     reverse(all(s));
  24.     return s;
  25. }
  26.  
  27. int main() {
  28.     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  29.  
  30.     int testcase;
  31.     cin >> testcase;
  32.     for (int test = 1; test <= testcase; test++) {
  33.         string s;
  34.         cin >> s;
  35.         vector<int> arr(100);
  36.         int p = 0;
  37.         for (int i = 0; i < sz(s); i++) {
  38.             if (s[i] == '+') arr[p] = (arr[p] + 1) % 256;
  39.             else if (s[i] == '-') arr[p] = (arr[p] - 1 + 256) % 256;
  40.             else if (s[i] == '>') p = (p + 1) % 100;
  41.             else if (s[i] == '<') p = (p - 1 + 100) % 100;
  42.         }
  43.         cout << "Case " << test << ": ";
  44.         for (int i = 0; i < 100; i++) {
  45.             cout << hexa(arr[i]) << " \n"[i == 99];
  46.         }
  47.     }
  48. }
  49.  
Tags: UVA CP3
Advertisement
Add Comment
Please, Sign In to add comment