华为OD机试D卷-特惠寿司(100分)JAVA

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int[] prices = Arrays.stream(in.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
        int[] sushi = solveSushi(prices);
        for (int i : sushi) {
            System.out.printf("%d ", i);
        }
    }

    private static int[] solveSushi(int[] prices) {
        int len = prices.length;
        int[] pricesDis = new int[len * 2];
        for (int i = 0; i < pricesDis.length; i++) {
            pricesDis[i] = prices[i % len]; //给新数组赋值,模拟转盘
        }
        for (int i = 0; i < len; i++) {
            for (int j = i + 1; j < i + len; j++) {
                if (pricesDis[j] < pricesDis[i]) {
                    prices[i] = pricesDis[j] + pricesDis[i];
                    break;
                }
            }
        }
        return prices;
    }
}

全部评论

相关推荐

1 收藏 评论
分享
牛客网
牛客企业服务