3169:【例28.3】 数列分段

3169:【例28.3】 数列分段

信息学奥赛一本通-编程启蒙(C++版)在线评测系统

[例 28.3] 数列分段

1428:数列分段

信息学奥赛一本通(C++版)在线评测系统

28.3_哔哩哔哩_bilibili

1436:数列分段II

信息学奥赛一本通(C++版)在线评测系统




C语言代码:

#include<stdio.h>
#include<stdlib.h>
long long ans,num,sum=1,a[100000+100];
int main()
{	

	int i;
	
	scanf("%lld%lld",&ans,&num);

	for(int i=1;i<=ans;i++)
	{
		scanf("%lld",&a[i]);
	}
	
	for(i=1;i<=ans;i++)
	{
		if( a[i]+a[i+1]<=num )
		{
			a[i+1]+=a[i];
		}
		else
		{
			sum++;
		}
	}
	
	printf("%lld",sum);

    return 0;
}



C++代码:

//落谷 p1181
#include<bits/stdc++.h>
using namespace std;

const int maxn=100010;

int c[maxn];

int main()
{
    int n,m,Count;
    Count=1;
    scanf("%d %d",&n,&m);
    for(int i=0;i<n;++i){
        scanf("%d",&c[i]);
    }
    int tmp=0;//阶段和
    for(int i=0;i<n;++i){
        tmp+=c[i];
        if(tmp>m){
            Count++;
            tmp=0;
            tmp+=c[i];
        }
    }
    printf("%d\n",Count);
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
int main()
{	
	long long ans,num,sum=1,a[100000+100];
	cin>>ans>>num;
	
	for(int i=1;i<=ans;i++)
	{
		cin>>a[i];
	}
	
	for(int i=1;i<=ans;i++)
	{
		if( a[i]+a[i+1]<=num )
		{
			a[i+1]+=a[i];
		}
		else
		{
			sum++;
		}
	}
	
	cout<<sum<<endl;
	
    return 0;
}



python3代码:

cnt=0

sum=0

n,m=input().split()
n,m=int(n),int(m)

for i in input().split():
    a=int(i)
    if sum+a>m:
        cnt+=1
        sum=a
    else:
        sum+=a

print(cnt+1)



 




阅读程序 提高篇(1-8题)

阅读程序 提高篇(1-8题)-CSDN博客

第三章 阅读程序 提高篇 4、【NOIP2001】

第三章 阅读程序 提高篇 4、【NOIP2001】-CSDN博客

阅读程序 提高篇(9-14题)

阅读程序 提高篇(9-14题)_xtorseaample-CSDN博客

阅读程序 提高篇 10.[NOIP2004]

阅读程序 提高篇 10.[NOIP2004]-CSDN博客

NOIP 提高组 初赛 阅读程序写结果

NOIP 提高组 初赛 阅读程序写结果_noip 提高组 第一轮 阅读程序-CSDN博客

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

dllglvzhenfeng

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

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

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

打赏作者

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

抵扣说明:

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

余额充值