赞
踩
寿司店周年庆,正在举办优惠活动回馈新老客户。
寿司转盘上总共有 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+" "); } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。