fork download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5. class Main
  6. {
  7. public static void main (String[] args) throws java.lang.Exception
  8. {
  9. Scanner in = new Scanner(System.in);
  10. int n = Integer.parseInt(in.next());
  11. char[] tape = new char[n];
  12. for (int i = 0; i < n; i++)
  13. tape[i] = 0;
  14. int pointer = 0;
  15. String program = in.next();
  16. for (int i = 0; i < program.length(); i++) {
  17. if (program.charAt(i) == '>')
  18. pointer++;
  19. if (program.charAt(i) == '<')
  20. pointer--;
  21. if (program.charAt(i) == '+')
  22. tape[pointer]++;
  23. if (program.charAt(i) == '-')
  24. tape[pointer]--;
  25. if (program.charAt(i) == '.')
  26. System.out.print(tape[pointer]);
  27. if (program.charAt(i) == ',')
  28. tape[pointer] = (char) System.in.read();
  29.  
  30. if (program.charAt(i) == '[') {
  31. if (tape[pointer] == 0) {
  32. int counter = 1;
  33. while (counter > 0) {
  34. i++;
  35. if (program.charAt(i) == '[')
  36. counter++;
  37. if (program.charAt(i) == ']')
  38. counter--;
  39. }
  40. }
  41. }
  42. else if (program.charAt(i) == ']') {
  43. if (tape[pointer] != 0) {
  44. int counter = 1;
  45. while (counter > 0) {
  46. i--;
  47. if (program.charAt(i) == '[')
  48. counter--;
  49. if (program.charAt(i) == ']')
  50. counter++;
  51. }
  52. }
  53. }
  54. }
  55. }
  56. }
Success #stdin #stdout 0.14s 321344KB
stdin
100
++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.
stdout
Hello World!