C++编程信奥赛一本通:1031-1032-1033-1034-1035


1031-反向输出一个三位数

将一个三位数反向输出,例如输入358,反向输出853。

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

int main() {
  int a;
  cin >> a;
  while (a != 0) {
    cout << a % 10;
    a /= 10;
  }
  return 0;
}

1032-大象喝水

一只大象口渴了,要喝20升水才能解渴,但现在只有一个深h厘米,底面半径为r厘米的小圆桶(h和r都是整数)。问大象至少要喝多少桶水才会解渴。

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

const double PI = 3.14;
int main() {
  int h, r;
  cin >> h >> r;
  cout << ceil(20 * 1000 / (h * PI * r * r)); // 向上取整
  return 0;
}

1033-计算线段长度

已知线段的两个端点的坐标A(Xa,Ya),B(Xb,Yb),求线段AB的长度,保留到小数点后3位。

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

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

  double xa, ya, xb, yb;
  cin >> xa >> ya >> xb >> yb;
  cout << sqrt(pow(xa - xb, 2) + pow(ya - yb, 2));
  return 0;
}

1034-计算三角形面积

平面上有一个三角形,它的三个顶点坐标分别为(x1,y1),(x2,y2),(x3,y3),那么请问这个三角形的面积是多少,精确到小数点后两位。

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

int main() {
  cout.flags(ios::fixed);
  cout.precision(2);
  
  float x1, y1, x2, y2, x3, y3;
  cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3;
  //fabs 求绝对值
  cout << fabs (x1 * y2 + x2 * y3 + x3 * y1 - x1 * y3 - x2 * y1 - x3 * y2)/2.0;
  
  return 0;
}

或者用海伦公式求解

#include <cmath>
#include <iostream>
using namespace std;

int main() {
  cout.flags(ios::fixed);
  cout.precision(2);
  
  double x1, y1, x2, y2, x3, y3, a, b, c, p;
  cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3;
  a = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)); //|a|
  b = sqrt((x3 - x1) * (x3 - x1) + (y3 - y1) * (y3 - y1)); //|b|
  c = sqrt((x3 - x2) * (x3 - x2) + (y3 - y2) * (y3 - y2)); //|c|
  p = (a + b + c) / 2;
  cout << sqrt(p * (p - a) * (p - b) * (p - c)) << endl;
  
  return 0;
}

1035-等差数列末项计算

给出一个等差数列的前两项a1,a2,求第 n 项是多少。

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

int main() {
  int a, b, n;
  cin >> a >> b >> n;
  int d = b - a;
  cout << a + (n - 1) * d;
  return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值