pat-1104 Sum of Number Segments(20)(万万想不到之又是找规律系列)

 

Given a sequence of positive numbers, a segment is defined to be a consecutive subsequence. For example, given the sequence { 0.1, 0.2, 0.3, 0.4 }, we have 10 segments: (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) and (0.4).

Now given a sequence, you are supposed to find the sum of all the numbers in all the segments. For the previous example, the sum of all the 10 segments is 0.1 + 0.3 + 0.6 + 1.0 + 0.2 + 0.5 + 0.9 + 0.3 + 0.7 + 0.4 = 5.0.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N, the size of the sequence which is no more than 10​5​​. The next line contains N positive numbers in the sequence, each no more than 1.0, separated by a space.

Output Specification:

For each test case, print in one line the sum of all the numbers in all the segments, accurate up to 2 decimal places.

Sample Input:

4
0.1 0.2 0.3 0.4

Sample Output:

5.00

作者: CAO, Peng

单位: Google

时间限制: 200 ms

内存限制: 64 MB

代码长度限制: 16 KB

 

当我自认为找到规律美滋滋时,一个测试点没过给了我当头一棒,我觉得完全没道理扣我分呀...希望有高手路过能看一眼,点破这其中之奥妙。

【我的思路】

说一下我找的规律,以sample为例,10种排列分别如下
1
1 2
1 2 3
1 2 3 4......1//三角形的序号

2
2 3
2 3 4......2

3 
3 4......3

4......4

然后我惊奇的发现,这些三角形可以看作全部是由第一个三角形变换来的,可以发现三角形2是由三角形1减去第一竖列的1得到的,后面的以此类推,所以只要算每个三角形减去的数字的部分就好了。

 

【17分代码】

#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <vector>
#define ll long long
using namespace std;
const int maxn = 1e5 + 5;
double a[maxn],sum[maxn],ans[maxn];
double total = 0;
int main(){
    memset(a,0,sizeof(a));
    memset(sum,0,sizeof(sum));
    memset(ans,0,sizeof(ans));
    int n;
    scanf("%d",&n);
    for(int i = 0 ; i < n;i++)
    {
        scanf("%lf",&a[i]);
        sum[i] = sum[i -1] + a[i];
        //printf("%.2lf\n",sum[i]);
    }
    for(int i = 0;i < n;i++)
    {
        total += sum[i];
    }
    total = total * n;
    //printf("%.2lf\n",total);
    for(int i = 0 ; i < n ; i ++)
    {
        if(i > 0)
        {
            ans[i] = ans[i -1] - a[i - 1] * (n - i + 1);
           // printf("%.2lf\n",ans[i]);
        }
    }
    for(int i = 0;i < n;i++)
    {
        total += ans[i];
    }
    printf("%.2lf\n",total);
    return 0;
}

书上把解题的规律说的很详细了,我直接贴上来。

【通过代码】 

#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <vector>
#define ll long long
using namespace std;
const int maxn = 1e5 + 5;
int main(){
    double x,sum = 0;
    int n;
    scanf("%d",&n);
    for(int i = 1 ; i <= n;i++)
    {
        scanf("%lf",&x);
        sum += x * i *( n - i + 1);
    }
    
    printf("%.2lf\n",sum);
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值