1012--乙级

该C++程序读取一串数字,统计其中能被5和2同时整除的数的总和及数量,正负交替的数的差值及出现次数,偶数位置的数的个数,平均值以及最大值。程序通过输入控制台进行数据交互,并以特定格式输出结果。
#include<iostream>
#include<string>
#include <iomanip>
#include<sstream>
using namespace std;

int main()
{
    int noUse = 0;
    cin >> noUse;
    string s;
    getline(cin, s);
    int a1 = 0, a2 = 0, a3 = 0, a4 = 0, a5 = 0;
    int t, num1 = 0, num2 = 0, tag2 = 0, num3 = 0, max5 = 0;
    double sum4 = 0.0, num4 = 0.0, aver4 = 0.0;
    for (istringstream sin(s); sin >> t;) {
        
        if (t % 5 == 0 && t % 2 == 0) {
            num1 += t;
            a1++;
        }
        if (t % 5 == 1) {

            if (tag2 == 0) {
                num2 += t;
                tag2 = 1;
                continue;  //跳过下面这个if
            }
            if (tag2 == 1) {
                num2 -= t;
                tag2 = 0;
            }
            a2++;
        }
        if (t % 5 == 2) {
            num3++;
            a3++;
        }
        if (t % 5 == 3) {
            num4++;
            sum4 += t;
            a4++;
        }
        if (t % 5 == 4) {
            if (t > max5) {
                max5 = t;
            }
            a5++;
        }


    } 
    aver4 = sum4 / num4;
    if (a1 == 0) {
        cout << "N" << " ";
    }
    else {
        cout << num1 << " ";
    }
    if (a2 == 0) {
        cout << "N" << " ";
    }
    else {
        cout << num2 << " ";
    }
    if (a3 == 0) {
        cout << "N" << " ";
    }
    else {
        cout << num3 << " ";
    }
    if (a4 == 0) {
        cout << "N" << " ";
    }
    else {
        cout << fixed << setprecision(1) << aver4 << " ";
    }
    if (a5 == 0) {
        cout << "N";
    }
    else {
        cout << max5;
    }
    return 0;
}
### 关于PAT乙级1020题月饼问题的解答 此题目旨在解决如何基于给定的不同种类月饼的库存量、总售价以及市场的最大需求量,计算能够获得的最大收益。对于此类优化问题,可以采用贪心算法求解。 #### 贪心算法思路 为了最大化收益,应当优先考虑单价较高的月饼。因此,首先需要计算每种月饼的单位价格(即总价除以数量),并将这些月饼按照单位价格降序排列。接着,在满足市场需求的前提下尽可能多地购买高价值密度的商品[^1]。 #### 实现方法 具体来说,可以通过以下Python代码实现上述逻辑: ```python def max_profit(n, d, stocks, prices): # 计算每种商品的价值密度,并将其与对应的索引一起存储在一个列表中 value_density = [(prices[i]/stocks[i], i) for i in range(len(stocks))] # 将所有商品按价值密度从大到小排序 sorted_items = sorted(value_density, reverse=True) total_value = 0 remaining_demand = d for vd, idx in sorted_items: if remaining_demand <= 0: break available_stock = min(remaining_demand, stocks[idx]) total_value += available_stock * vd remaining_demand -= available_stock return round(total_value, 2) if __name__ == "__main__": n, d = map(int, input().split()) stock_quantities = list(map(float, input().strip().split())) sale_prices = list(map(float, input().strip().split())) result = max_profit(n, d, stock_quantities, sale_prices) print(f"{result:.2f}") ``` 该程序接收标准输入作为参数,其中`n`代表月饼种类的数量,而`d`则指代市场上的最大需求量。之后两行分别包含了各种类月饼的具体存量及其对应的价格信息。最后输出的结果为可达到的最大利润值,保留两位小数[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

二零二三.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值