cmath
库部分函数:
/*
double sin(double x)
double cos(double x)
double exp(double x)
double log(double x)
double pow(double x,double y)
double sqrt(double x)
double fabs(double x)
int abs(int)
double hypot(double, double)
double ceil(double x)
double floor(double x)
*/
#include<iostream>
#include<cmath>
using namespace std;
#define PI acos(-1.0)
int main(){
cout<<sin(PI/2)<<" "<<exp(1)<<" "<<log2(9)<<endl;
//虽然返回类型为double,但能计算出整数的会输出整数
cout<<ceil(3.5)<<" "<<floor(3.5)<<" "<<log(10)<<endl;
cout<<5e+6/3<<endl;//浮点数默认保留6位有效数字
cout<<round(-5.4); //表示“四舍五入”,算法为floor(x+0.5)
return 0;
}
运行输出:
牛吃草问题:
#include<iostream>
using namespace std;
int main(){
double n1,t1,n2,t2;//n1 != n2
double n3;
cin>>n1>>t1>>n2>>t2;
cin>>n3;
double v = (n1*t1 - n2*t2)/(t1 - t2);
double origin = n1*t1 - v*t1;
double t3 = origin/(n3 - v);
cout<< t3;
}