当前位置:   article > 正文

转盘寿司---循环的转盘

转盘寿司---循环的转盘

寿司店周年庆,正在举办优惠活动回馈新老客户。

寿司转盘上总共有 n 盘寿司,prices[i] 是第 i 盘寿司的价格,

如果客户选择了第 i 盘寿司,寿司店免费赠送客户距离第 i 盘寿司最近的下一盘寿司 j,前提是 prices[j] < prices[i],如果没有满足条件的 j,则不赠送寿司。

每个价格的寿司都可无限供应。

输入描述
输入的每一个数字代表每盘寿司的价格,每盘寿司的价格之间使用空格分隔,例如:

3 15 6 14

表示:

第 0 盘寿司价格 prices[0] 为 3
第 1 盘寿司价格 prices[1] 为 15
第 2 盘寿司价格 prices[2] 为 6
第 3 盘寿司价格 prices[3] 为 14
寿司的盘数 n 范围为:1 ≤ n ≤ 500

每盘寿司的价格 price 范围为:1 ≤ price ≤ 1000

输出描述
输出享受优惠后的一组数据,每个值表示客户选择第 i 盘寿司时实际得到的寿司的总价格。使用空格进行分隔,例如:

3 21 9 17

用例1
输入
3 15 6 14
输出
3 21 9 17

import java.util.ArrayList;
import java.util.Scanner;
public class Main{
    public static void main(String[] args){
       Scanner in = new Scanner(System.in);
       ArrayList<Integer> foods = new ArrayList<Integer>();
       // 存储输入的 寿司的价格
       while(in.hasNextInt()){
          int temp = in.nextInt();
          foods.add(temp);
       }

       for(int i=0;i<foods.size();i++){
         // 构造不同起点的 “队列”  
          int []prices = new int[foods.size()];
          int t = 0;
          for(int j=i;j<foods.size();j++){
            prices[t] = foods.get(j);
            t++;
          } 
          for(int k=0;k<i;k++){
            prices[t] = foods.get(k);
            t++;
          }

          // 计算选择第I盘寿司时得到的总价格
          int sum = prices[0];
          int u = 1;
          while(u<prices.length){
            if(prices[u] < prices[0]){
               sum+=prices[u];
               break;
            }
            else{
               u++;
            }
          }
          System.out.print(sum+" ");
       }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/622311
推荐阅读
相关标签
  

闽ICP备14008679号