C++中三个数比较大小(包含相等的情况)

        在学习C++时,网上关于三个数比大小的问题很多没有涉及到其中两个数相等或者三个数相等的情况。我自己写了一段代码共道友们参考,有不足之处或有可改进之处望大家指正。

        代码中涉及的知识点有C++的输入输出函数,IF条件判断等。

        

#include<iostream>
using namespace std;

int main()
{
	double A, B, C;
	cout << "请输入数字A" << endl;
	cin >> A;
	cout << "请输入数字B" << endl;
	cin >> B;
	cout << "请输入数字C" << endl;
	cin >> C;

	if (A > B)
	{
	
		if (A > C)
		{
			cout << "A最大" << endl;
		}
		else if(A < C) {
			cout << "C最大" << endl;
		}
		else {
			cout << "A=C>B" << endl;
		}
	}
	else if(A < B)
	{
		if (B > C)
		{
			cout << "B最大" << endl;
		}
		else if(B<C)
		{
			cout<<"C最大"<<endl;
		}
		else {
			cout << "B=C>A" << endl;
		}
		
	}
	else {
		if (A > C) {
			cout << "A=B>C" << endl;
		}
		else if (A < C) {
			cout << "C最大" << endl;
		}
		else {
			cout << "A=B=C" << endl;
		}
	}




	system("pause");
	return 0;
}

 有不足之处或有可改进之处望大家指正!

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以使用 C 语言的 qsort() 函对坐标组进行排序。qsort() 函需要三个:需要排序的组的首地址、元素的个数比较元素大小的函。 示例代码: ``` #include <stdio.h> #include <stdlib.h> struct Point { int x; int y; }; int compare(const void* a, const void* b) { struct Point *pa = (struct Point *)a; struct Point *pb = (struct Point *)b; return (pa->x - pb->x); } int main() { struct Point points[] = {{3, 4}, {1, 2}, {5, 6}, {7, 8}}; int n = sizeof(points) / sizeof(points[0]); qsort(points, n, sizeof(struct Point), compare); for (int i = 0; i < n; i++) { printf("%d, %d\n", points[i].x, points[i].y); } return 0; } ``` 这段代码将定义一个结构体 Point 用来存储坐标点的 x 和 y 坐标,然后使用 qsort() 函将点按照 x 坐标从小到大排序。compare 函是用来比较两个点 x 坐标的大小的,它是 qsort() 函需要的第三个。 ### 回答2: 要实现对一组无序的坐标组根据x的大小进行从小到大排序,可以使用C语言的快速排序算法来实现。 快速排序算法的基本思想是通过一趟排序将待排序的记录划分成独立的两部分,其一部分记录的关键字<=另一部分记录的关键字,然后再分别对这两部分记录进行排序,直到整个序列有序。 首先,定义一个结构体类型来表示坐标,其包含x和y坐标的成员变量。 ```c typedef struct { int x; int y; } Coordinate; ``` 然后,实现一个比较,用于确定两个坐标的大小关系。我们按照x的大小进行比较,如果x相等,则按y的大小进行比较。 ```c int compare(const void *a, const void *b) { Coordinate *coordinateA = (Coordinate *)a; Coordinate *coordinateB = (Coordinate *)b; if (coordinateA->x == coordinateB->x) { return coordinateA->y - coordinateB->y; } else { return coordinateA->x - coordinateB->x; } } ``` 最后,在主函创建一个Coordinate组,并调用qsort函进行排序。 ```c int main() { Coordinate coordinates[] = {{3, 4}, {1, 2}, {5, 6}, {2, 3}, {4, 5}}; int numCoordinates = sizeof(coordinates) / sizeof(coordinates[0]); qsort(coordinates, numCoordinates, sizeof(Coordinate), compare); for (int i = 0; i < numCoordinates; i++) { printf("(%d, %d) ", coordinates[i].x, coordinates[i].y); } return 0; } ``` 运行以上代码,可以输出排序后的坐标组。 这样,我们就通过C语言实现了对一组无序的坐标组根据x的大小进行从小到大排序。 ### 回答3: 要使用C语言实现无序坐标组根据x的大小进行从小到大排序,可以使用冒泡排序算法来实现。冒泡排序是一种简单但效率较低的排序算法,其基本思想是通过反复交换相邻的元素,依次比较相邻的两个元素大小,从而将最大(或最小)的元素逐渐交换到最后的位置。 下面是一个简单的C语言代码实现: ```c #include <stdio.h> // 定义一个坐标结构体 typedef struct { int x; int y; } Coordinate; // 冒泡排序函 void bubbleSort(Coordinate arr[], int size) { int i, j; for (i = 0; i < size - 1; i++) { for (j = 0; j < size - i - 1; j++) { if (arr[j].x > arr[j + 1].x) { // 交换位置 Coordinate temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } } int main() { // 定义一个无序的坐标组 Coordinate coords[] = {{5, 2}, {7, 3}, {2, 1}, {8, 4}, {3, 6}}; int size = sizeof(coords) / sizeof(coords[0]); // 调用冒泡排序函进行排序 bubbleSort(coords, size); // 输出排序后的结果 printf("排序后的坐标:\n"); for (int i = 0; i < size; i++) { printf("(%d, %d) ", coords[i].x, coords[i].y); } printf("\n"); return 0; } ``` 以上代码,我们首先定义了一个坐标的结构体`Coordinate`,有两个成员变量`x`和`y`。然后实现了冒泡排序函`bubbleSort`,内部使用两层循环依次比较并交换相邻的元素。在主函`main`初始化了一个无序的坐标组`coords`,然后调用冒泡排序函对其进行排序。最后输出排序后的结果。 这样,我们就可以通过C语言实现对无序坐标组根据x的大小进行从小到大排序的功能了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值