【ACM】- PAT. A1104 & B1049 Sum of Number Segments 【数学问题 - 找规律】

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_26398495/article/details/82383641
题目链接
题目分析

给出一段序列,求出所有子序列的数值和!

解题思路:寻找规律

在序列下方用横线画出所有集合,就能很明显看出规律;
i个数,出现(N - i + 1)次,共出现i组!


AC程序(C++)
/**********************************
*@ID: 3stone
*@ACM: PAT.A1104 Sum of Number Segments
*@Time: 18/8/26
*@IDE: VSCode 2018 + clang++
***********************************/
#include<cstdio>
using namespace std;
const int maxn = 100010;
double number[maxn];

int main() {

    int n;
    while(scanf("%d", &n) != EOF) {
        double sum = 0;
        for(int i = 1; i <= n; i++){
            scanf("%lf", &number[i]);
        }

        for(int i = 1; i <= n; i++) {
            sum += (number[i] * i * (n - i + 1));
        }

        printf("%.2f\n", sum);

    }//while

    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_26398495/article/details/82383641