1049 数列的片段和 (20 point(s))
给定一个正数数列,我们可以从中截取任意的连续的几个数,称为片段。例如,给定数列 { 0.1, 0.2, 0.3, 0.4 },我们有 (0.1) (0.1, 0.2) (0.1, 0.2, 0.3) (0.1, 0.2, 0.3, 0.4) (0.2) (0.2, 0.3) (0.2, 0.3, 0.4) (0.3) (0.3, 0.4) (0.4) 这 10 个片段。
给定正整数数列,求出全部片段包含的所有的数之和。如本例中 10 个片段总和是 0.1 + 0.3 + 0.6 + 1.0 + 0.2 + 0.5 + 0.9 + 0.3 + 0.7 + 0.4 = 5.0。
输入格式:
输入第一行给出一个不超过 105 的正整数 N,表示数列中数的个数,第二行给出 N 个不超过 1.0 的正数,是数列中的数,其间以空格分隔。
输出格式:
在一行中输出该序列所有片段包含的数之和,精确到小数点后 2 位。
输入样例:
4
0.1 0.2 0.3 0.4
输出样例:
5.00
经验总结:
找规律的题目,还是很考验临场综合能力的,这一题我也找到了规律,计算比例数组,但是没想到比例数组竟然有超过整型的数,大意了....定义为long long int型就可以通过了~当然,看了书上的才发现.....我想的还是复杂了,还是书上的简单....
AC代码
我自己写的复杂的= =:
#include <cstdio>
#include <cstring>
long long port[100010];
void dispose(int n)
{
port[0]=n;
for(int i=1;i<=n/2;++i)
{
port[i]=port[i-1]+n-2*i;
}
for(int i=n-1;i>=n/2;--i)
{
port[i]=port[n-1-i];
}
}
int main()
{
int n;
double answer,temp;
while(~scanf("%d",&n))
{
dispose(n);
answer=0;
for(int i=0;i<n;++i)
{
scanf("%lf",&temp);
answer+=temp*port[i];
}
printf("%.2f\n",answer);
}
return 0;
}
书上的简单规律:
#include <cstdio>
#include <cstring>
int main()
{
int n;
double answer,temp;
while(~scanf("%d",&n))
{
answer=0;
for(int i=1;i<=n;++i)
{
scanf("%lf",&temp);
answer+=temp*i*(n+1-i);
}
printf("%.2f\n",answer);
}
return 0;
}