teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

ソースの追加

2018/04/05 08:09

投稿

退会済みユーザー
answer CHANGED
@@ -60,4 +60,61 @@
60
60
  }
61
61
  }
62
62
 
63
+ ```
64
+ <追記> 符号が複数の場合のソースコード
65
+ ```Java
66
+ import java.util.*;
67
+
68
+ public class Main {
69
+ public static void main(String[] args) {
70
+
71
+ Scanner sc = new Scanner(System.in);
72
+ String line = sc.nextLine();
73
+ int count = count_sign(line) + 1;
74
+ int []number = new int[count];
75
+ for(int i = 0; i < count; i++){
76
+ number[i] = 0; /*初期化*/
77
+ }
78
+ int position = 0;
79
+ int sign = 1;
80
+ for(int i = 0; i < line.length(); i++){
81
+ char c = line.charAt(i);
82
+ if(c == '>'){
83
+ number[position] += sign;
84
+ }
85
+ else if(c == '<'){
86
+ number[position] += 5 * sign;
87
+ }
88
+ else if(c == '+'){
89
+ sign = 1;
90
+ position++;
91
+ }
92
+ else if(c == '-'){
93
+ sign = -1;
94
+ position++;
95
+
96
+ }
97
+ }
98
+ int sum = 0;
99
+
100
+ for(int i = 0; i < count; i++){
101
+ sum += number[i];
102
+ }
103
+
104
+ System.out.println(sum);
105
+
106
+ }
107
+
108
+ public static int count_sign(String line){
109
+ int count = 0;
110
+ for(int i = 0; i < line.length(); i++){
111
+ char c = line.charAt(i);
112
+ if(c == '+' || c == '-'){
113
+ count++;
114
+ }
115
+ }
116
+ return count;
117
+ }
118
+ }
119
+
63
120
  ```

1

ソースの変更

2018/04/05 08:09

投稿

退会済みユーザー
answer CHANGED
@@ -17,15 +17,14 @@
17
17
  以下、ソースコードとなります。
18
18
 
19
19
  ```Java
20
- import java.io.BufferedReader;
20
+ import java.util.*;
21
- import java.io.InputStreamReader;
22
21
 
23
22
  public class Main {
24
- public static void main(String[] args) throws Exception {
23
+ public static void main(String[] args) {
25
24
  // Your code here!
26
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
25
+ Scanner sc = new Scanner(System.in);
27
- String line = br.readLine();
26
+ String line = sc.nextLine();
28
- int []number = {0,0}; //説明の2番目
27
+ int []number = {0,0};//説明の2番目
29
28
  int position = 0;
30
29
  for(int i = 0; i < line.length(); i++){
31
30
  char c = line.charAt(i);