用C语言实现:Bear_2 除了生产小熊饼干外,又新生产了菱形饼干和正方形饼干。但是一号车间质检员张*蛋是个憨憨,他只会判断他们的边长来确定形状,众所周知这是不行的。为了让自己不失业她求助于你,请你帮忙判断一下它是哪块小饼干? 输入格式: 按顺时针输入。 输入包括 4 行,第 i 行给出两个整数 X i ,Y i (−100<=X i ,Y i <=100) 保证输入的四条边长相同 输出格式: 如果它是一个菱形饼干请输出"LX",否则请输出"ZFX",不带引号。
时间: 2024-03-16 20:42:16 浏览: 19
以下是用 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 是一个非常小的数,用于比较两个浮点数是否相等,可以避免浮点数精度误差带来的问题。
相关问题
使用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 x[4], y[4];
double sides[4];
for (int i = 0; i < 4; i++) {
scanf("%d%d", &x[i], &y[i]);
}
for (int i = 0; i < 4; i++) {
sides[i] = sqrt(pow(x[(i+1)%4]-x[i], 2) + pow(y[(i+1)%4]-y[i], 2));
}
if (fabs(sides[0] - sides[2]) < 1e-6 && fabs(sides[1] - sides[3]) < 1e-6 && fabs(sides[0] * sqrt(2) - sides[1]) < 1e-6) {
printf("ZFX\n");
} else if (fabs(sides[0] - sides[2]) < 1e-6 && fabs(sides[1] - sides[3]) < 1e-6) {
printf("LX\n");
} else {
printf("ERROR\n");
}
return 0;
}
```
与之前的Python和C++代码类似,核心思路也是一样的,只是语法有所不同:
- 使用scanf函数读入四个点的坐标;
- 使用pow函数计算平方;
- 使用fabs函数计算绝对值。
完整代码输出结果如下:
输入:
```
0 0
0 1
1 1
1 0
```
输出:
```
ZFX
```
请用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
以下是使用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"。
相关推荐
![](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)