1049

// 超时啦!
//

#include "stdafx.h"
#include
#include
#include
#include       //cout格式化输出!



using namespace std;

int main()
{
    unsigned n;
    cin >> n;

    double tmp;
    vector data;
    for (unsigned i = 0; i < n; ++i)
    {
        cin >> tmp;
        data.push_back(tmp);
    }

    double sum = 0.0;
    for (size_t i = 0; i < n; ++i)
    {
        vector tmp_data;
        tmp_data.push_back(data[i]);
        sum =sum+ accumulate(tmp_data.begin(), tmp_data.end(),0.0); //第三个实参决定了容器的类型。所累加对象必须与第三个参数类型一直!
        for (size_t j = i + 1; j < n; ++j)
        {
            tmp_data.push_back(data[j]);
            sum = sum+accumulate(tmp_data.begin(), tmp_data.end(), 0.0);
        }
    }

    cout.setf(ios::fixed);
    cout << setprecision(2) << sum;

    system("pause");
    return 0;
}

// 正确姿势!每个元素累加的次数存在规律!
//

#include "stdafx.h"
#include
#include
#include
#include       //cout格式化输出!



using namespace std;

int main()
{
    unsigned n;
    cin >> n;

    double tmp;
    vector data;
    for (unsigned i = 0; i < n; ++i)
    {
        cin >> tmp;
        data.push_back(tmp);
    }

    double sum = 0.0;
    for (size_t i = 1, j = n; i <= n; ++i, --j)
    {
        sum = sum + i*j*data[i-1];   //(第i-1号元素,累加的次数为i*j!)
    }

    cout.setf(ios::fixed);
    cout << setprecision(2) << sum;

    system("pause");
    return 0;
}

你可能感兴趣的:(1049)