|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
题目是
给定平面上任意三个点的坐标(x1,y1)(x2,y2​)(x3,y3),检验它们能否构成三角形。
输入格式:
输入在一行中顺序给出六个[−100,100]范围内的数字,即三个点的坐标x1,y1,x2,y2,x3,y3
输出格式:
若这3个点不能构成三角形,则在一行中输出“Impossible”;若可以,则在一行中输出该三角形的周长和面积,格式为“L = 周长, A = 面积”,输出到小数点后2位。
- #include<stdio.h>
- #include<math.h>
- int main()
- {
- double x1,x2,x3,y1,y2,y3;
- scanf("%lf%lf%lf%lf%lf%lf",&x1,&y1,&x2,&y2,&x3,&y3);
- double a,b,c,max,t,m;
- a=pow((x1-x2),2)+pow((y1-y2),2);
- b=pow((x3-x2),2)+pow((y3-y2),2);
- c=pow((x1-x3),2)+pow((y1-y3),2);
- m=fabs(x1*(y2-y3)+x2*(y1-y3)+x3*(y1-y2))/2;
- if(a<b)
- {
- t=a;a=b;b=t;
- }
- if(a<c)
- {
- t=a;a=c;c=t;
- }
- if(b<c)
- {
- t=b;b=c;c=t;
- }
- if((a>b+c)||(a<fabs(b-c))) printf("Impossible");
- else printf("L = %.2lf, A = %.2lf",a+b+c,m);
-
- }
复制代码
这是一个判断三角形能否构成的问题,需要判断任意两边之和是否大于第三边,并且判断三个点是否共线。下面是三份代码,其中第一份是 C 语言编写的,第二份和第三份是 Python 语言编写的。
C 语言:
- #include <stdio.h>
- #include <math.h>
- int main() {
- double x1, x2, x3, y1, y2, y3;
- scanf("%lf%lf%lf%lf%lf%lf", &x1, &y1, &x2, &y2, &x3, &y3);
- double a, b, c, max, t, m;
- a = sqrt(pow((x1 - x2), 2) + pow((y1 - y2), 2));
- b = sqrt(pow((x3 - x2), 2) + pow((y3 - y2), 2));
- c = sqrt(pow((x1 - x3), 2) + pow((y1 - y3), 2));
- m = fabs(x1 * (y2 - y3) + x2 * (y1 - y3) + x3 * (y1 - y2)) / 2;
- if (a < b) {
- t = a;
- a = b;
- b = t;
- }
- if (a < c) {
- t = a;
- a = c;
- c = t;
- }
- if (b < c) {
- t = b;
- b = c;
- c = t;
- }
- if ((a > b + c) || (a < fabs(b - c))) {
- printf("Impossible");
- } else {
- printf("L = %.2lf, A = %.2lf", a + b + c, m);
- }
- return 0;
- }
复制代码
Python:
- import math
- x1, y1, x2, y2, x3, y3 = map(float, input().split())
- a = math.sqrt((x1 - x2) [b] 2 + (y1 - y2) [/b] 2)
- b = math.sqrt((x2 - x3) [b] 2 + (y2 - y3) [/b] 2)
- c = math.sqrt((x3 - x1) [b] 2 + (y3 - y1) [/b] 2)
- if a + b > c and a + c > b and b + c > a:
- perimeter = a + b + c
- s = (a + b + c) / 2
- area = math.sqrt(s * (s - a) * (s - b) * (s - c))
- print("L = %.2f, A = %.2f" % (perimeter, area))
- else:
- print("Impossible")
复制代码- import math
- x1, y1, x2, y2, x3, y3 = map(float, input().split())
- a = math.sqrt((x1 - x2) [b] 2 + (y1 - y2) [/b] 2)
- b = math.sqrt((x2 - x3) [b] 2 + (y2 - y3) [/b] 2)
- c = math.sqrt((x3 - x1) [b] 2 + (y3 - y1) [/b] 2)
- m = abs(x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)) / 2
- if a + b > c and a + c > b and b + c > a:
- perimeter = a + b + c
- s = (a + b + c) / 2
- area = math.sqrt(s * (s - a) * (s - b) * (s - c))
- print("L = %.2f, A = %.2f" % (perimeter, m))
- else:
- print("Impossible")
复制代码
注意事项:
1. 在 C 语言编写的程序中,需要使用 pow 函数计算平方,而在 Python 中可以直接使用 ** 运算符;
2. 在 Python 中,需要使用 abs 函数计算绝对值,而在 C 语言和 Python 中都可以使用 fabs 函数;
3. 在 Python 中,需要使用 % 运算符和格式化字符串来输出结果。
此内容为ChatGPT(AI)回答,若有错误多多包涵,如果对你有帮助,请设置一个最佳答案!
|
|