1 题目
2 代码
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class A1046 {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] strings = br.readLine().split(" ");
int N = Integer.parseInt(strings[0]);
int[] nExits = new int[N + 1];
int[] distance = new int[N + 1];
int sum = 0; // 依次记录初始入口到其他入口的距离,最后记录总距离(第一个入口和第N个入口相邻)
for (int i = 1; i <= N; i++) {
nExits[i] = Integer.parseInt(strings[i]);
sum += nExits[i];
distance[i] = sum;
}
int M = Integer.parseInt(br.readLine());
for (int i = 0; i < M; i++) {
String[] line = br.readLine().split(" ");
int m = Integer.parseInt(line[0]);
int n = Integer.parseInt(line[1]);
if (m > n) {
m = m + n;
n = m - n;
m = m - n;
}
int pre = distance[n - 1] - distance[m - 1];
int next = sum - (distance[n - 1] - distance[m - 1]);
System.out.println(Math.min(pre, next));
}
}
}
3 要点
(1)输入的5 1 2 4 14 9,5表示共5个入口,而1 2 4 14 9则依次表示1-2,2-3,。。。,5-1之间的距离。
(2)利用BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
读入信息。