题目描述:
寿司店周年庆,正在举办优惠活动回馈新老客户。寿司转盘上总共有 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 14 15 6 5
输出:3 20 21 11 8
C++源码:
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
using namespace std;
vector<int> cakeDiscount(int N, vector<int> price) {
vector<int> result;
for (int i = 0; i < N - 1; i++) {
int priceOne = price[i];
bool tag = true;
for (int j = i + 1; j < N; j++) {
if (price[j] < priceOne) {
result.push_back(price[j]);
tag = false;
break;
}
}
if (tag) {
result.push_back(0);
}
}
rlt.push_back(0);
return rlt;
}
int main() {
string line;
getline(cin, line);
stringstream ss(line);
vector<int> prices;
int temp;
while (ss >> temp) {
prices.push_back(temp);
}
int N0 = prices.size();
prices.insert(prices.end(), prices.begin(), prices.end());
int N = prices.size();
vector<int> discount = cakeDiscount(N, prices);
vector<int> realPrice;
for (int i = 0; i < N; i++) {
realPrice.push_back(prices[i] + discount[i]);
}
for (int i = 0; i < N0; i++) {
cout << realPrice[i] << " ";
}
cout << endl;
system("pause");
return 0;
}