首页 > 编程语言 >小美走公路(美团2024届秋招笔试第一场编程真题)

小美走公路(美团2024届秋招笔试第一场编程真题)

时间:2024-04-03 22:13:13浏览次数:34  
标签:小美 scanner 真题 int 届秋招 long forward nextInt roads

题面

核心思想

正走一遍,反走一遍。

代码

import java.util.*;

public class Main {
    public static void main(String[] args) {
        final long MOD = (long) (1e9 + 7);
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int[] roads = new int[n];
        for(int i = 0; i < n; i++){
            roads[i] = scanner.nextInt();
        }
        int x = scanner.nextInt();
        int y = scanner.nextInt();

        long forward = 0, backward = 0;
        for(int i = x - 1; i != y - 1; i = (i + 1) % n){
            forward += roads[i];
        }

        for(int i = x - 1; i != y - 1; i = (i - 1 + n) % n){
            // roads 记录的是正走的距离 反走的距离应该看前一个road[i-1] 
            backward += roads[(i - 1 + n) % n];
        }

        System.out.println(Math.min(forward, backward));
    }

}

标签:小美,scanner,真题,int,届秋招,long,forward,nextInt,roads
From: https://www.cnblogs.com/ganyq/p/18113609

相关文章