#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>
using namespace std;
double computePay(int n, double a[], double sv) {
double total = 0.0;
for (int i = 0; i < n; ++i) {
total += a[i];
}
if (n >= 5 && total >= sv) {
double minPrice = *std::min_element(a, a + n);
total -= minPrice;
}
return total;
}
int main() {
int n;
double sv;
cin >> n >> sv;
vector<double> prices(n);
for (int i = 0; i < n; ++i) {
std::cin >> prices[i];
}
double result = computePay(n, prices.data(), sv);
cout << fixed << setprecision(2) << result <<endl;
return 0;
}
computePay 函数
参数:
n: 商品的数量。
a: 商品价格的数组。
sv: 最低支付金额。
功能:
计算所有商品的总价格。
如果 n 大于等于5且总价格大于等于 sv,则从总价格中减去最便宜的商品的价格。
返回最终的支付金额。
main 函数
功能:
从用户输入中读取商品数量 n 和最低支付金额 sv。
读取每个商品的价格并存储在 vector<double> 中。
调用 computePay 函数计算最终支付金额。
以固定的小数点后两位格式输出最终支付金额。
#include <vector>
#include <algorithm>
#include <iomanip>
using namespace std;
double computePay(int n, double a[], double sv) {
double total = 0.0;
for (int i = 0; i < n; ++i) {
total += a[i];
}
if (n >= 5 && total >= sv) {
double minPrice = *std::min_element(a, a + n);
total -= minPrice;
}
return total;
}
int main() {
int n;
double sv;
cin >> n >> sv;
vector<double> prices(n);
for (int i = 0; i < n; ++i) {
std::cin >> prices[i];
}
double result = computePay(n, prices.data(), sv);
cout << fixed << setprecision(2) << result <<endl;
return 0;
}
computePay 函数
参数:
n: 商品的数量。
a: 商品价格的数组。
sv: 最低支付金额。
功能:
计算所有商品的总价格。
如果 n 大于等于5且总价格大于等于 sv,则从总价格中减去最便宜的商品的价格。
返回最终的支付金额。
main 函数
功能:
从用户输入中读取商品数量 n 和最低支付金额 sv。
读取每个商品的价格并存储在 vector<double> 中。
调用 computePay 函数计算最终支付金额。
以固定的小数点后两位格式输出最终支付金额。
全部评论
相关推荐
点赞 评论 收藏
分享
04-03 17:47
北京中南海业余大学 Java AI牛可乐:哇,听起来你很激动呢!杭州灵枢维度科技听起来很厉害呀~你逃课去白马培训,老冯会同意吗?不过既然你这么感兴趣,肯定是有原因的吧!
对了,想了解更多关于这家公司或者求职相关的问题吗?可以点击我的头像私信我哦,我可以帮你更详细地分析一下!
点赞 评论 收藏
分享

