PAT (Basic Level) Practice (中文)- 1049 数列的片段和(20 分)

简介: PAT (Basic Level) Practice (中文)- 1049 数列的片段和(20 分)

题目链接:点击打开链接

题目大意:略。

解题思路:


1、每个 a[i] 右边出现过几次,左边出现过几次,右边规律好找比较稳定,然后用总次数手算出来除以右边的次数,发现左边的规律也来了。

2、注意:变量计算的顺序不一样会导致溢出的情况。

AC 代码

#include<bits/stdc++.h>
#include<cmath>
#define mem(a,b) memset(a,b,sizeof a);
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        double a,rs=0;
        for(int i=0;i<n;i++)
        {
            scanf("%lf",&a);
            rs+=a*(i+1)*(n-i);   // 不会溢出
//            rs+=(i+1)*(n-i)*a; // 会溢出
        }
        printf("%.2f\n",rs);
    }
    return 0;
}
目录
相关文章
|
C语言 C++
PAT (Basic Level) Practice (中文)1099 性感素数(20分)
“性感素数”是指形如 (p, p+6) 这样的一对素数。之所以叫这个名字,是因为拉丁语管“六”叫“sex”(即英语的“性感”)。(原文摘自 http://mathworld.wolfram.com/SexyPrimes.html) 现给定一个整数,请你判断其是否为一个性感素数。
109 0
|
测试技术
PAT (Basic Level) Practice (中文)1012 数字分类 (20 分)+易错测试点
PAT (Basic Level) Practice (中文)1012 数字分类 (20 分)+易错测试点
106 0
PAT (Basic Level) Practice (中文)1012 数字分类 (20 分)+易错测试点
|
人工智能 测试技术
PAT (Basic Level) Practice (中文) B1008 数组元素循环右移问题 (20 分)
PAT (Basic Level) Practice (中文) B1008 数组元素循环右移问题 (20 分)
79 0
PAT (Basic Level) Practice (中文) B1008 数组元素循环右移问题 (20 分)
|
测试技术
PAT (Basic Level) Practice (中文) B1011 A+B 和 C (15 分)
PAT (Basic Level) Practice (中文) B1011 A+B 和 C (15 分)
83 0
PAT (Basic Level) Practice (中文) B1011 A+B 和 C (15 分)
|
算法
PAT (Basic Level) Practice (中文)1028. 人口普查(20分)
PAT (Basic Level) Practice (中文)1028. 人口普查(20分)
85 0
|
存储 测试技术
PAT (Basic Level) Practice (中文) 1004 成绩排名 (20 分)
PAT (Basic Level) Practice (中文) 1004 成绩排名 (20 分)
69 0
PAT (Basic Level) Practice (中文) B1046 划拳 (15 分)
PAT (Basic Level) Practice (中文) B1046 划拳 (15 分)
65 0
|
C语言
PAT (Basic Level) Practice (中文) B1026 程序运行时间 (15 分)
PAT (Basic Level) Practice (中文) B1026 程序运行时间 (15 分)
100 0
|
编译器
PAT (Basic Level) Practice (中文)- 1077 互评成绩计算(20 分)
PAT (Basic Level) Practice (中文)- 1077 互评成绩计算(20 分)
87 0
|
存储 人工智能
PAT (Basic Level) Practice (中文)- 1030 完美数列(25 分)
PAT (Basic Level) Practice (中文)- 1030 完美数列(25 分)
75 0