6.1 函数(1) (素数、牛顿迭代法求平方根)

#include<iostream>
using namespace std;
int Max(int x, int y)
{
	if(x > y) 
		return x;
	else 
		return y;
}
int main()
{
	int n = Max(4,6);//6 
	cout << n << "," << Max(20,n) << endl;//6,20
	return 0;
} 

#include<iostream>
#include<cmath>
using namespace std;
bool Isprime(unsigned int n)//无符号整数,一定为正数
{
	if(n <=1 ) 
		return false;
	for(int i = 2; i <= sqrt(n); ++i)
	{
		if(n % i == 0)
			return false;
	}
	return true;
}
int main()
{
	 cout << Isprime(2) << "," << Isprime(4) << "," << Isprime(5);//1,0,1 
	 return 0; 
}

#include<iostream>
#define EPS 0.001//精度
using namespace std;
double Sqrt(double a)
{
	double x = a/2, lastx = x + 1 + EPS;
	while(x - lastx > EPS || lastx - x < EPS)//|x-lastx|>EPS大于精度范围,就要继续迭代 
	{
		lastx = x;
		x = (x + a/x)/2;
	}
	return x; 
}
double Distance(double x1, double y1, double x2, double y2)
{
	return Sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
} 
int main()
{
	double x1, y1, x2, y2, x3, y3;
	cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3;
	cout << Distance(x1,y1,x2,y2) << endl;
	cout << Distance(x1,y1,x3,y3) << endl;
	cout << Distance(x2,y2,x3,y3) << endl;
	return 0;
}



猜你喜欢

转载自blog.csdn.net/yanyanwenmeng/article/details/81058131