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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值