用LUA(和C++)刷PAT (Advanced Level) ——1085 Perfect Sequence

该程序实现了一个寻找给定整数数组中,乘以一个给定值P后能形成的最大连续子序列和的算法。首先读取数组大小N和乘数值P,然后对数组进行排序。通过遍历排序后的数组,动态计算每个子序列的和,更新最大子序列和。最终输出最大子序列和。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;

int main()
{
    int N, P, Max = 0;
    cin>>N>>P;
    vector<long long int> numbers(N);
    for(int i = 0; i < N; i++)
        scanf("%d", &(numbers[i]));
    sort(numbers.begin(), numbers.end());
    auto end_itr = numbers.begin();
    for(auto start_itr = numbers.begin(); start_itr != numbers.end(); start_itr ++){
        long long int limit = *start_itr * P;
        while(end_itr != numbers.end() && *end_itr <= limit)
            end_itr ++;
        Max = max(Max, (int)(end_itr-start_itr));
    }
    cout<<Max<<endl;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值