常见的算法

常见的算法

1.两点间距离

1
2
3
sqrt( pow( abs(x1-x2) ,2 ) + pow( abs(y1-y2) ) , 2 )    //sqrt()开平方根
//pow(x,y) 得到x的y次方
也可以:sqrt( (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) ) //abs() 得到参数的绝对值

2.求质数

1
2
3
4
5
6
7
8
9
// x = a * b  ->  min(a,b) <=sqrt(x)
int chk(int x){
for(int i = 2;i * i <= x;++i){
if(x % i == 0){
return 0;
}
}
return 1;
}

3.对数组进行重复且不叠加的修改时

1.可以令数组全为0,令被修改的数为1
1
2
eg:	int a[n] = {0};
i符合条件: a[i] = 1
2.可以用bool 定义数组
1
2
eg:  bool a[n];
i符合条件:a[i] = 1;

4.输出小数

1
cout << fixed <<  setprecision(1) << res << '\n';

常见的算法
https://hainoir.github.io/posts/b74716b.html
作者
hainoir
发布于
2026年2月5日
许可协议