c++基础-数学相关应用

1.最大公约数

//辗转相除法
int gcd(int a,int b){
if(b==0) return a;
return gcd(b,a%b);
}

2.最小公倍数

int min_ab(int a,int b){
return a*b/gcd(a,b);
}
//a,b最小公倍数=a*b/最大公约数

3.素数

bool is_prime(int n){
for(int i=2;i*i<=n;i++){
   if(n%i==0) return false;
  }
return n!=1;
}

4.数学函数
头文件:#include <cmath>或者#include <math.h>

  • 绝对值
    int型,double型,long型
int abs(int arg);
double fabs(double arg);
long labs(long arg);
  • 求幂
double exp(double arg);
//返回e的arg次幂
  • 求上界、下界
//返回不小于num的最小整数
double ceil(double num);
//返回不大于num的最大整数
double floor(double num);

  • 求指数
double pow(double a,double b);
//返回a^b
  • 求平方根
double sqrt(double num);

5.四舍五入骚操作

int n,roundn;
cin>>n;
int roundn=n/2+n%2;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值