1.
浮点数判断相等时,不可直接用等号比较
double a,b;
错误:if(a == b)…
解决方法1:
if( abs(a - b) < 1E-10 )
解决方法2:
使用整数替代浮点数
Code1:
#include <iostream>
#include <cmath>
using namespace std;
const double pj = 2.3;
const double yl = 1.9;
const double total = 82.3;
const int maxn = 100;
int main(){
int i,j;
for(i = 0; i < maxn; ++i){//饮料数量
for(j = 0; j < i; ++j){//啤酒数量
if(abs(yl * i + pj * j - total) < 1E-10){
cout<<i<<endl;
break;
}
}
}
return 0;
}
Code2:
#include <iostream>
#include <cmath>
using namespace std;
const int pj = 23;
const int yl = 19;
const int total = 823;
const int maxn = 100;
int main(){
int i,j;
for(i = 0; i < maxn; ++i){//饮料数量
for(j = 0; j < i; ++j){//啤酒数量
if(yl * i + pj * j == total){
cout<<i<<endl;
break;
}
}
}
return 0;
}
2.比酒量
#include <iostream>
#include <cmath>
using namespace std;
const int maxn = 20;
int main(){
double a,b,c,d;
for(double a = maxn; a >= 1; --a){
for(double b = a - 1; b >= 1; --b){
for(double c = b - 1; c >= 1; --c){
for(double d = c - 1; d >= 1; --d){
if(abs(1/a + 1/b + 1/c + 1/d - 1) < 1E-10){
cout<<a<<" "<<b<<" "<<c<<" "<<d<<endl;
}
}
}
}
}
return 0;
}
3.为了高精度的表示有理数,有理数可表示成 分子/分母
//分数的四则运算
//求最大公约数
#include <iostream>
#include <cmath>
using namespace std;
struct Fraction{
int up,down;
}fraction;
//辗转相除法求最大公约数
int gcd(int x, int y){
if(x < 0) x = -x;
if(y < 0) y = -y;
if(y == 0) return x;
return gcd(y, x % y);
}
//求最大公倍数
int lcm(int x, int y){
int d = gcd(x, y);
return x * y / d;
}
Fraction reduction(Fraction a){
if(a.down < 0){
a.down = -a.down;
a.up = -a.up;
}
if(a.up == 0){
a.down = 1;
}
else{
int d = gcd(a.up, a.down);
a.up /= d;
a.down /= d;
}
return a;
}
Fraction add(Fraction a, Fraction b){
Fraction res;
res.down = a.down * b.down;
res.up = a.down * b.up + a.up * b.down;
return reduction(res);
}
Fraction sub(Fraction a, Fraction b){
Fraction res;
res.down = a.down * b.down;
res.up = a.up * b.down - a.down * b.up;
return reduction(res);
}
int main(){
Fraction a,b;
a.up = 1,a.down = 5;
b.up = 1,b.down = 15;
Fraction res = add(a,b);
cout<<res.up<<"/"<<res.down<<endl;
res = sub(a,b);
cout<<res.up<<"/"<<res.down<<endl;
return 0;
}
4.
#include <iostream>
#include <cmath>
using namespace std;
int main(){
// 在进行高精度时的要求
// 例如要求有效数字100位(任意精度的浮点数)
// double IEEE 754
// 5个特殊值:0.0 inf -1 NaN(Not a Number不是一个数值)
// cout<<3 / 0<<endl; 错误
cout<<3.0 / 0<<endl;
double a = 3.0 / 0;//有效的浮点值
cout<<"a = "<<a<<endl;
cout<<"a + 1 = "<<a + 1<<endl;
cout<<"a + a = "<<a + a<<endl;
cout<<"1 / a = "<<1 / a<<endl;
cout<<"1 / a * (-1) = "<<1 / a * (-1)<<endl;
cout<<"a * (-1) = "<<a * (-1)<<endl;
cout<<"a - a = "<<a - a<<endl;
cout<<"a / a = "<<a / a<<endl;
return 0;
}
5.数学上的运算:比较关键
//与浮点数有关的,舍入模式不同导致结果不同
//看清答案要求的舍入模式
四舍五入
四舍六入五成双:5舍去or入使得结果取偶数
例题:
几何问题:出现几率较少,不好算..容易算错
四舍五入保留两个小数,数据保证答案精确值的小数点后第三位不是4或5
#include <iostream>
#include <cmath>
using namespace std;
int main(){
//四舍五入
cout<<round(3.5)<<endl;
cout<<round(3.4)<<endl;
//强制类型四舍五入
cout<<(int)(3.5 + 0.5)<<endl;
//强制类型,丢弃小数部分
cout<<(int)(3.5)<<endl;
//向上取整
cout<<ceil(3.5)<<endl;
//向下取整
cout<<floor(3.5)<<endl;
//求绝对值函数 浮点型
cout<<fabs(-3.00005)<<endl;
cout<<abs( -3.00005)<<endl;
return 0;
}