版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/piaoliangjinjin/article/details/82426750
给定一个正数数列,我们可以从中截取任意的连续的几个数,称为片段。例如,给定数列 { 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
分析:
这道题很简单,但是需要注意两点:
(1) 数组的类型选择double,选择int和float都是错误的。
(2)输出格式: cout << fixed << setprecision(2) << sum <<endl;
上代码:
// B49.cpp : Defines the entry point for the console application.
//
#include<cstdio>
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int n;
cin >> n;
double a[n];
double sum = 0;
for (int i = 0; i < n; i++)
{
cin >> a[i];
sum = sum + (i+1) *a[i] * (n - i);
}
cout << fixed << setprecision(2) << sum <<endl;
return 0;
}