2071-2072-2073-1029-1030

阅读量: 220 编辑

2071-【例2.14】平均分

已知某班有男同学x位,女同学y位,x位男生平均分是87分,y位女生的平均分是85,问全体同学平均分是多少分?

//爱码岛编程 
#include <iostream>
using namespace std;

int main() {
  cout.flags(ios::fixed);
  cout.precision(4);
  
  int x, y;
  cin >> x >> y;
  cout << double(x * 87 + y * 85) / (x + y);
  return 0;
}

2072-【例2.15】歌手大奖赛

歌手大奖赛上6名评委给一位参赛者打分,6个人打分的平均分为9.6分;如果去掉一个最高分,这名参赛者的平均分为9.4分;如果去掉一个最低分,这名参赛者的平均分为9.8分;如果去掉一个最高分和一个最低分,这名参赛者的平均是多少?

//爱码岛编程 
#include <iostream>
#include <iomanip>
using namespace std;

int main() {
  cout.flags(ios::fixed);
  cout.precision(2);

  double t, h, l; // t:总分,h:最高分,l:最低分
  t = 6 * 9.6;
  h = t - 5 * 9.4;
  l = t - 5 * 9.8;
  cout << setw(5) << (t - h - l) / 4;
  return 0;
}

2073-【例2.16 】三角形面积

传说古代的叙拉古国王海伦二世发现的公式,利用三角形的三条边长来求取三角形面积。已知△ABC中的三边长分别为a,b,c,求△ABC的面积。

提示:海伦公式

//爱码岛编程 
#include <cmath>
#include <iostream>
using namespace std;

int main() {
  cout.flags(ios::fixed);
  cout.precision(3);

  double a, b, c, p, s;
  cin >> a >> b >> c;
  p = (a + b + c) / 2;
  s = sqrt(p * (p - a) * (p - b) * (p - c));
  cout << s;
  return 0;
}

1029-计算浮点数相除的余

计算两个双精度浮点数a和b的相除的余数,a和b都是双精度浮点数。这里余数(r)的定义是:a=k×b+r,其中k是整数,0≤r<b。

//爱码岛编程 
#include <iostream>
using namespace std;

int main() {
  double a, b;
  cin >> a >> b;
  int k = int(a / b);
  cout << a - k * b;
  return 0;
}

1030-计算球的体积

对于半径为 r 的球,其体积的计算公式为V=(4/3) * πr3,这里取 π=3.14。现给定 r,即球半径,类型为double,求球的体积V,保留到小数点后2位。

//爱码岛编程
#include <iostream>
using namespace std;

int main() {
  cout.flags(ios::fixed);
  cout.precision(2);
  double r;
  cin >> r;
  cout << 4.0 * 3.14 * r * r * r / 3;
  return 0;
}

爱码岛编程公众号
试卷资料
爱码岛编程小程序
在线刷题
苏ICP备13052010号
©2023 南京匠成信息科技有限公司