乙级1049,浮点数相乘顺序影响精度

Ac的代码

#include 
using namespace std;
const int N=100010;
double a[N];
int main(){
    int n;
    scanf("%d",&n);
    double v,ans=0;
    for (int i = 1; i <=n ; ++i) {
        scanf("%lf",&v);
        ans =ans+v*i*(n+1-i);
    }
    printf("%.2f",ans);
    return 0;
}

wa的代码

#include 
using namespace std;
const int N=100010;
double a[N];
int main(){
    int n;
    scanf("%d",&n);
    double v,ans=0;
    for (int i = 1; i <=n ; ++i) {
        scanf("%lf",&v);
        ans =ans+i*(n+1-i)*v;
    }
    printf("%.2f",ans);
    return 0;
}

你可能感兴趣的:(pat)