请用c语言来解决下面问题:Bear_2 除了生产小熊饼干外,又新生产了菱形饼干和正方形饼干。但是一号车间质检员张*蛋是个憨憨,他只会判断他们的边长来确定形状,众所周知这是不行的。为了让自己不失业她求助于你,请你帮忙判断一下它是哪块小饼干? 输入格式: 按顺时针输入。 输入包括 4 行,第 i 行给出两个整数 X i ,Y i (−100<=X i ,Y i <=100) 保证输入的四条边长相同 输出格式: 如果它是一个菱形饼干请输出"LX",否则请输出"ZFX",不带引号。 输入样例: 0 0 0 1 1 1 1 0 输出样例: ZFX
时间: 2024-03-18 18:39:37 浏览: 22
以下是使用C语言解决Bear_2小饼干形状判断问题的代码:
```c
#include <stdio.h>
#include <math.h>
int main()
{
int x1, y1, x2, y2, x3, y3, x4, y4;
double d1, d2, d3, d4;
scanf("%d %d %d %d %d %d %d %d", &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4);
d1 = sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2));
d2 = sqrt(pow(x2 - x3, 2) + pow(y2 - y3, 2));
d3 = sqrt(pow(x3 - x4, 2) + pow(y3 - y4, 2));
d4 = sqrt(pow(x4 - x1, 2) + pow(y4 - y1, 2));
if (d1 == d2 && d2 == d3 && d3 == d4) {
printf("ZFX\n");
} else {
printf("LX\n");
}
return 0;
}
```
其中,d1、d2、d3、d4分别表示输入的4条边的长度。如果这4条边的长度都相等,则输出"ZFX",否则输出"LX"。
相关问题
请用c语言代码实现下面问题:Bear_2 除了生产小熊饼干外,又新生产了菱形饼干和正方形饼干。但是一号车间质检员张*蛋是个憨憨,他只会判断他们的边长来确定形状,众所周知这是不行的。为了让自己不失业她求助于你,请你帮忙判断一下它是哪块小饼干? 输入格式: 按顺时针输入。 输入包括 4 行,第 i 行给出两个整数 X i ,Y i (−100<=X i ,Y i <=100) 保证输入的四条边长相同 输出格式: 如果它是一个菱形饼干请输出"LX",否则请输出"ZFX",不带引号。 输入样例: 0 0 0 1 1 1 1 0 输出样例: ZFX 代码长度限制 16 KB 时间限制 400 ms 内存限制 64 MB
以下是 C 语言代码实现:
```c
#include <stdio.h>
#include <math.h>
int main() {
int x1, y1, x2, y2, x3, y3, x4, y4;
scanf("%d%d%d%d%d%d%d%d", &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4);
double d1 = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
double d2 = sqrt((x2 - x3) * (x2 - x3) + (y2 - y3) * (y2 - y3));
double d3 = sqrt((x3 - x4) * (x3 - x4) + (y3 - y4) * (y3 - y4));
double d4 = sqrt((x4 - x1) * (x4 - x1) + (y4 - y1) * (y4 - y1));
if (d1 == d2 && d2 == d3 && d3 == d4) {
double d = sqrt((x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3));
if (d1 == d && d2 == d && d3 == d && d4 == d)
printf("LX\n");
else
printf("ZFX\n");
} else {
printf("ZFX\n");
}
return 0;
}
```
用C语言实现:Bear_2 除了生产小熊饼干外,又新生产了菱形饼干和正方形饼干。但是一号车间质检员张*蛋是个憨憨,他只会判断他们的边长来确定形状,众所周知这是不行的。为了让自己不失业她求助于你,请你帮忙判断一下它是哪块小饼干? 输入格式: 按顺时针输入。 输入包括 4 行,第 i 行给出两个整数 X i ,Y i (−100<=X i ,Y i <=100) 保证输入的四条边长相同 输出格式: 如果它是一个菱形饼干请输出"LX",否则请输出"ZFX",不带引号。
以下是用 C 语言实现的代码:
```c
#include <stdio.h>
#include <math.h>
int main() {
int points[4][2]; // 存放4个点的坐标
double d1, d2; // 存放对角线长度
// 输入4个点的坐标
for (int i = 0; i < 4; i++) {
scanf("%d%d", &points[i][0], &points[i][1]);
}
// 计算对角线长度
d1 = sqrt(pow(points[0][0]-points[2][0], 2) + pow(points[0][1]-points[2][1], 2));
d2 = sqrt(pow(points[1][0]-points[3][0], 2) + pow(points[1][1]-points[3][1], 2));
// 判断形状并输出结果
if (fabs(d1 - d2) < 1e-7) { // 对角线长度相等,为正方形
printf("ZFX\n");
} else { // 对角线长度不相等,为菱形
printf("LX\n");
}
return 0;
}
```
其中,pow() 函数用于计算平方, fabs() 函数用于返回一个浮点数的绝对值。1e-7 是一个非常小的数,用于比较两个浮点数是否相等,可以避免浮点数精度误差带来的问题。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)