MCPcopy Create free account
hub / github.com/algorithmzuo/algorithm-journey / main

Method main

src/class153/Code04_Bookcase1.java:160–197  ·  view source on GitHub ↗
(String[] args)

Source from the content-addressed store, hash-verified

158 }
159
160 public static void main(String[] args) {
161 Kattio io = new Kattio();
162 n = io.nextInt();
163 m = io.nextInt();
164 add(0);
165 for (int i = 1; i <= n; i++) {
166 add(io.nextInt());
167 }
168 add(n + 1);
169 // 注意在最左插入了0,最右插入了n+1,作为准备值,所以一共n+2个数
170 // 下面操作时,不要忘了最左是0,最右是n+1,并且永远不修改
171 n = n + 2;
172 String op;
173 for (int i = 1, s, t, rank; i <= m; i++) {
174 op = io.next();
175 s = io.nextInt();
176 rank = ask(s) + 1;
177 if (op.equals("Top")) {
178 // 因为有最左侧的准备值,所以开头是中序排名2的位置
179 move(rank, 2);
180 } else if (op.equals("Bottom")) {
181 // 因为有最右侧的准备值,所以结尾是中序排名n-1的位置
182 move(rank, n - 1);
183 } else if (op.equals("Insert")) {
184 t = io.nextInt();
185 move(rank, rank + t);
186 } else if (op.equals("Ask")) {
187 // rank代表当前数字的排名,因为有最左侧的准备值
188 // 所以排名其实是rank-1,题目要返回小于的数量,所以是rank - 2
189 io.println(rank - 2);
190 } else {
191 // 因为有最左侧的准备值,所以查s+1名的数字
192 io.println(query(s + 1));
193 }
194 }
195 io.flush();
196 io.close();
197 }
198
199 // Kattio类IO效率很好,但还是不如StreamTokenizer
200 // 只有StreamTokenizer无法正确处理时,才考虑使用这个类

Callers

nothing calls this directly

Calls 10

nextIntMethod · 0.95
addMethod · 0.95
nextMethod · 0.95
askMethod · 0.95
moveMethod · 0.95
queryMethod · 0.95
equalsMethod · 0.80
printlnMethod · 0.45
flushMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected