ZYF最喜欢的活动是走楼梯!所以他每次去实验室总会坐电梯随机到一个楼层,然后走楼梯到实验室所在的楼层,并为此得意不已。现在的问题来了,已经知道每两层楼之间的楼梯级数、ZYF坐电梯要到达的楼层、实验室所在楼层,那么ZYF每次得走多少级楼梯才能到达实验室?
每组测试数据有两行:第一行为三个整数N, A, B(0<N≤100, 0<A, B≤N),表示有N层楼,ZYF坐电梯到的楼层A,实验室所在楼层为B;第二行包括N–1个整数,其中第i个整数代表从第i层到第i + 1层之间的楼梯级数Si(0<Si≤100)。
6 1 5
10 10 10 10 10
6 5 1
10 10 10 10 10
10 3 7
1 2 3 4 5 6 7 8 9
40
18
===============================================================
package algorithm;
import java.util.Scanner;
public class xidian003 {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
int num; //测试数据的组数
int n,a,b; //楼的层数、坐电梯到达的层、目的地
num=scanner.nextInt();
int count; //需要走的台阶数
int step[]=new int[100];
for (int i = 0; i < num; i++) {
n=scanner.nextInt();
a=scanner.nextInt();
b=scanner.nextInt();
count=0;
for (int j = 0; j < n-1; j++) {
step[j]=scanner.nextInt();
}
if (a==b) {
System.out.println(0);
}else if(a<b){
for (int j = a-1; j < b-1; j++) {
//System.out.println("step[]"+j+" :"+step[j]);
count+=step[j];
}
System.out.println(count);
}else {
for (int j = b-1; j < a-1; j++) {
//System.out.println("step[]"+j+" :"+step[j]);
count+=step[j];
}
System.out.println(count);
}
}
}
}