3169:【例28.3】 数列分段
[例 28.3] 数列分段
1428:数列分段
1436:数列分段II
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题)
第三章 阅读程序 提高篇 4、【NOIP2001】
阅读程序 提高篇(9-14题)
阅读程序 提高篇 10.[NOIP2004]
NOIP 提高组 初赛 阅读程序写结果