[AOJ]Lesson - ITP1 Topic # 10 Math Functions 数学函数

A: Distance

Write a program which calculates the distance between two points P1(x1, y1) and P2(x2, y2).

Input

Four real numbers x1, y1, x2 and y2 are given in a line.

Output

Print the distance in real number. The output should not contain an absolute error greater than 10^-4.

Sample Input

0 0 1 1

Sample Output

1.41421356

题目大意

计算两点P1(x1, y1)和P2(x2, y2)之间的距离。输入输出是实数类型,绝对值误差不能超过10^-4.

#include <bits/stdc++.h>
using namespace std;
int main(){
	double x1, y1, x2, y2;
	cin >> x1 >> y1 >> x2 >> y2;
	double dis = sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
	cout << fixed << setprecision(5) << dis;  // 保留到第5位小数即可保证正确性
	return 0;
}

B: Triangle

For given two sides of a triangle a and b and the angle C between them, calculate the following properties:

S: Area of the triangle
L: The length of the circumference of the triangle
h: The height of the triangle with side a as a bottom edge

Input

The length of a, the length of b and the angle C are given in integers.

Output

Print S, L and h in a line respectively. The output should not contain an absolute error greater than 10-4.

Sample Input

4 3 90

Sample Output

6.00000000
12.00000000
3.00000000

题目大意

给出三角形的两边a,b及两边夹角C(度数计算)。计算三角形的面积、周长、以a为底的高。误差不能超过10^-4。

// 三角形的数学公式:正弦、余弦公式
#include <bits/stdc++.h>
using namespace std;
int main(){
	double a, b, c, ang, s, l, h;
	const double Pi = 3.1415926;
	cin >> a >> b >> ang;
	ang = Pi*ang/180;  // 角度转化弧度 
	s = a*b*sin(ang)/2.0;  // a*sin(ang)是以b为底的高 
	c = sqrt(a*a+b*b-2*a*b*cos(ang));  // 余弦定理 
	l = a+b+c;
	h = 2*s/a;
	cout << fixed << setprecision(5) << s << endl;
	cout << fixed << setprecision(5) << l << endl;
	cout << fixed << setprecision(5) << h << endl;
	return 0;
}

C: Standard Deviation

You have final scores of an examination for n students. Calculate standard deviation of the scores s1, s2 ... sn.

The variance α2 is defined by α2 = (∑ni=1(si - m)2)/n

where m is an average of si. The standard deviation of the scores is the square root of their variance.

Input

The input consists of multiple datasets. Each dataset is given in the following format:

n
s1 s2 ... sn
The input ends with single zero for n.

Output

For each dataset, print the standard deviation in a line. The output should not contain an absolute error greater than 10-4.

Constraints

n ≤ 1000
0 ≤ si ≤ 100

Sample Input

5
70 80 100 90 20
3
80 80 80
0

Sample Output

27.85677655
0.00000000

题目大意

求n个数的标准差,有若干组数据,最后一组数据以0结尾。

#include <bits/stdc++.h>
using namespace std;
int main(){
	int n;
	double a[1010], avg, ans;
	while(cin >> n){
		if(n==0)
			break;
		avg = ans = 0;
		for(int i=1; i<=n; i++){
			cin >> a[i];
			avg += a[i];
		}
		avg = 1.0*avg/n;
		for(int i=1; i<=n; i++)
			ans += 1.0*(avg-a[i])*(avg-a[i]);
		ans = sqrt(ans/n);
		cout << fixed << setprecision(5) << ans << endl;
	} 
	return 0;
}

D: Distance II

Your task is to calculate the distance between two n dimensional vectors x={x1,x2,...,xn} and y={y1,y2,...,yn}.

The Minkowski's distance defined below is a metric which is a generalization of both the Manhattan distance and the Euclidean distance.

where p=∞
Write a program which reads two n dimensional vectors x and y, and calculates Minkowski's distance where p=1,2,3,∞ respectively.

Input

In the first line, an integer n is given. In the second and third line, x={x1,x2,...xn} and y={y1,y2,...yn} are given respectively. The elements in x and y are given in integers.

Output

Print the distance where p=1,2,3 and ∞ in a line respectively. The output should not contain an absolute error greater than 10^-5.

Constraints

1≤n≤100
0≤xi,yi≤1000

Sample Input

3
1 2 3
2 0 4

Sample Output

4.000000
2.449490
2.154435
2.000000

题目大意

根据题目给出的计算公式,分别计算出p=1,2,3,∞时的距离。

#include <bits/stdc++.h>
using namespace std;
int main(){
	int n;
	double x[110], y[110];
	cin >> n;
	for(int i=1; i<=n; i++)
		cin >> x[i];
	for(int i=1; i<=n; i++)
		cin >> y[i];
	double s1 = 0;
	for(int i=1; i<=n; i++) // p=1
		s1 += abs(x[i]-y[i]);
	cout << fixed << setprecision(6) << s1 << endl;
	double s2 = 0;
	for(int i=1; i<=n; i++) // p=2
		s2 += (x[i]-y[i])*(x[i]-y[i]);
	s2 = sqrt(s2);
	cout << fixed << setprecision(6) << s2 << endl;
	double s3 = 0;
	for(int i=1; i<=n; i++) // p=3
		s3 += abs((x[i]-y[i])*(x[i]-y[i])*(x[i]-y[i]));
	s3 = pow(s3, 1.0/3);  // 开三次方,即1/3次方
	cout << fixed << setprecision(6) << s3 << endl;
	double s4 = 0;
	for(int i=1; i<=n; i++)
		s4 = max(s4, abs(x[i]-y[i]));
	cout << fixed << setprecision(6) << s4 << endl;
	return 0;
}
posted @ 2019-11-19 23:26  gdgzliu  阅读(578)  评论(0编辑  收藏  举报