题解【CF171C】A Piece of Cake

Description

给你(n)个数,求出(sum_{i=1}^{n} a_{i} imes iqquad)

Input

(n + 1)个数,分别为(n)(n)个数(a_{i})((1)(leq i le n))。

Output

(sum_{i=1}^{n} a_{i} imes iqquad)

Examples

Input

4 1 2 3 4

Output

30

Solution

根据题目翻译可知:这是一道模拟题。

首先,输入一个整数(n),接下来输入(n)个整数,分别为(a_{1}),(a_{2}), (cdots),(a_{n}) ,要你输出(ans) (=) (sum_{i=1}^{n} a_{i} imes iqquad)

我们可以用一层循环来枚举(i)(i)的范围从(1) ~ (n),每次输入(a_{i})时,就可以将答案(ans)增加(a_{i}) ( imes) (i),最后输出答案(ans)即可。

算法时间复杂度:(Theta(n)),空间复杂度:(Theta(1))

Code

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cctype>//头文件准备

using namespace std;//使用标准名字空间

inline int gi()//快速读入,不解释
{
    int f = 1, x = 0;
    char c = getchar();
    while (c < '0' || c > '9')
    {
        if (c == '-') f = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9')
    {
        x = x * 10 + c - '0';
        c = getchar();
    }
    return f * x;
}

int n, sum;//n为数字的个数,sum为答案

int main()//进入主函数
{
	n = gi();//输入数字个数
    for (int i = 1; i <= n; i++)
    {
    	int x = gi();//依次输入每个数
        sum = sum + i * x;//更新答案
    }
    printf("%d
", sum);//输出最终答案
    return 0;//完美结束
}
原文地址:https://www.cnblogs.com/xsl19/p/11104942.html