信息学奥赛系列教程:常用的数学函数

版权声明:本文为博主原创文章,未经允许不得转载 https://blog.csdn.net/noipBar/article/details/84578921

C++语言中常用的数学函数:

1、求绝对值 abs()        

2、求平方根sqrt()        

3、求指数pow()        

4、向下取整floor()

5、向上取整ceil()

使用这些数学函数,需要引用cmath头文件,上述函数中,只有pow需要两个参数,其余只需要一个参数

代码如下:

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
	int a=-5;
	cout<<"a的绝对值:"<<abs(a)<<endl;
	int b=16;
	cout<<"b的平方根:"<<sqrt(b)<<endl;
	float b1=8.52;
	cout<<"b1的平方根:"<<sqrt(b1)<<endl;
	int c=3,d=2;
	cout<<c<<"的"<<d<<"次方:"<<pow(c,d)<<endl;
	double pi =3.144654357;
	cout<<"pi向下取整是:"<<floor(pi)<<endl;
	cout<<"pi向上取整是:"<<ceil(pi)<<endl;
	cout<<fixed<<setw(10)<<setprecision(2)<<pi<<endl;
 	return 0;
}

常用数学函数练习:

代码:

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
	int a=27,b=34;
	int c,d;
	float e;
	float f,g;
	c = a*b;
	d = pow((a-b),3);
	e = (a-b)/(a+b);
	f = sqrt(a+b);
	g = c+d-e+f;
	cout<<g<<endl;
 	return 0;
}

猜你喜欢

转载自blog.csdn.net/noipBar/article/details/84578921