一、 abs和fabs函数区别
abs():求绝对值函数
fabs():求绝对值函数
abs( )主要用于对求整数的绝对值,在“stdlib.h”(或 )头文件里面。
而fabs( )主要是求精度要求更高的double ,float 型的绝对值,在头文件里。
两者在只#include时都可以使用
二、acos函数
(当然也存在asin()、atan()函数)
acos的参数是cos值,返回值是cos值对应角的弧度值,它的头文件是cmath,c++中使用三角函数时,角的表示方法都是弧度制的,传送参数和接受参数时要注意。
同时涉及这两个函数的例题:
https://sdnuoj.rainng.com/problem/show/1199
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e4 + 7;
#define inf 0x3f3f3f3f
int a, b, c;
float ansab, ansa, ansb, ansc, ans;
bool judge(int a, int b, int c){
//判断是不是三角形
if(a+b>c && a+c>b && b+c>a && abs(a-c)<b && abs(a-b)<c && abs(b-c)<a) return 1;
else return 0;
}
int main(){
// std::ios::sync_with_stdio(0);
// cin.tie(0);
// cout.tie(0);
while(cin >> a >> b >> c){
if(judge(a, b, c)){
ansa = acos((b*b + c*c - a*a) / double(2*b*c));
ansb = acos((a*a + c*c - b*b) / double(2*a*c));
ansc = acos((a*a + b*b - c*c) / double(2*a*b));
ansab = max(ansa, ansb);
ans = max(ansab, ansc);
printf("%.3lf\n", ans);
}
else cout << "Error" << endl;
}
return 0;
}
注意:如果使用了解除封印的这几行代码:
std::ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
就不能使用printf输出带几位小数的数。否则虽然编译执行不会报错,但是交到oj上会WA。
三、double、float输入输出
输入:double一定要用%lf,float一定要用%f
输出:double可以用%f,也可以用%lf。float一定要用%f
涉及一、三知识点的例题:
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=2003
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
double ans, n;
int main(){
while(cin >> n){
ans = fabs(n);
printf("%.2lf", ans);
cout << endl;
}
return 0;
}