网易我的世界论坛

标题: 有人要免费的C语言加权平均数计算的代码嘛 [打印本页]

作者: helloya    时间: 3 天前
标题: 有人要免费的C语言加权平均数计算的代码嘛
加权平均数计算代码:
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[])
{
int n;
scanf("%d", &n);
double a[n][2];
for (int i = 0; i < n; i++)
{
scanf("%lf", &a[i][0]);
scanf("%lf", &a[i][1]);
}
double w_1 = 0;
for (int i = 0; i < n; i++)
{
w_1 += a[i][0] * a[i][1];
}
double w_2 = 0;
for (int i = 0; i < n; i++)
{
w_2 += a[i][1];
}
double ans;
ans = w_1 / w_2;
printf("%.3lf", ans);
return 0;




欢迎光临 网易我的世界论坛 (http://mc.netease.com/) Powered by Discuz! X3.3