zoukankan      html  css  js  c++  java
  • PAT 甲级 1104. Sum of Number Segments (20) 【数学】

    题目链接

    https://www.patest.cn/contests/pat-a-practise/1104

    思路

    最容易想到的一个思路就是 遍历一下所有组合 加一遍
    但 时间复杂度 太大 会超时

    其实可以发现 每个数字的出现频率是有规律的

    比如
    4
    0.1 0.2 0.3 0.4

    这组数据

    0.1 4
    0.2 6
    0.3 6
    0.4 4

    然后 出现次数 刚好满足 (i + 1) * (n - i) 这个公式

    AC代码

    #include <cstdio>
    #include <cstring>
    #include <ctype.h>
    #include <cstdlib>
    #include <cmath>
    #include <climits>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <deque>
    #include <vector>
    #include <queue>
    #include <string>
    #include <map>
    #include <stack>
    #include <set>
    #include <numeric>
    #include <sstream>
    #include <iomanip>
    #include <limits>
    
    #define CLR(a) memset(a, 0, sizeof(a))
    #define pb push_back
    
    using namespace std;
    typedef long long ll;
    typedef long double ld;
    typedef unsigned long long ull;
    typedef pair <int, int> pii;
    typedef pair <ll, ll> pll;
    
    const double PI  = 3.14159265358979323846264338327;
    const double E   = exp(1);
    const double eps = 1e-6;
    
    const int INF  = 0x3f3f3f3f;
    const int maxn = 1e5 + 5;
    const int MOD  = 1e9 + 7;
    
    int main()
    {
        int n;
        double num;
        scanf("%d", &n);
        double sum = 0.0;
        for (int i = 0; i < n; i++)
        {
            scanf("%lf", &num);
            sum += num * (i + 1) * (n - i);
        }
        printf("%.2lf
    ", sum);
    }
  • 相关阅读:
    依赖注入方法
    依赖注入
    用spring来控制反转(ioc)
    ioc控制反转笔记
    写模块的流程例子
    淘淘商城笔记1
    二叉树的前序中序后序遍历
    专题2 二叉树(go)
    专题1:二分查找
    python自动化开发-[第三天]-编码,函数,文件操作
  • 原文地址:https://www.cnblogs.com/Dup4/p/9433190.html
Copyright © 2011-2022 走看看