输入3个整数a、b、c,按从大到小的顺序输出。要求通过指针实
时间: 2024-02-24 15:07:05 浏览: 35
可以通过指针来实现对三个整数的排序。以下是一个示例代码:
```cpp
#include <iostream>
using namespace std;
void sort(int *a, int *b, int *c) {
if (*a < *b) {
int temp = *a;
*a = *b;
*b = temp;
}
if (*a < *c) {
int temp = *a;
*a = *c;
*c = temp;
}
if (*b < *c) {
int temp = *b;
*b = *c;
*c = temp;
}
}
int main() {
int a, b, c;
cin >> a >> b >> c;
sort(&a, &b, &c);
cout << c << " " << b << " " << a << endl;
return 0;
}
```
该代码定义了一个sort函数,通过指针对三个整数进行排序。在main函数中,读入三个整数a、b、c,调用sort函数对它们进行排序,然后按从大到小的顺序输出它们。
相关问题
输入3个整数a、b、c,按从大到小的顺序输出。要求通过指针实现。
```c
#include <stdio.h>
void sort(int *a, int *b, int *c);
int main(){
int a, b, c;
printf("请输入3个整数:\n");
scanf("%d%d%d", &a, &b, &c);
sort(&a, &b, &c);
printf("从大到小的顺序为:%d %d %d\n", a, b, c);
return 0;
}
void sort(int *a, int *b, int *c){
int temp;
if (*a < *b){
temp = *a;
*a = *b;
*b = temp;
}
if (*a < *c){
temp = *a;
*a = *c;
*c = temp;
}
if (*b < *c){
temp = *b;
*b = *c;
*c = temp;
}
}
```
输出示例:
```
请输入3个整数:
5 1 8
从大到小的顺序为:8 5 1
```
c++输入3个整数a、b、c,按从大到小的顺序输出。要求通过指针实现
```c
#include <stdio.h>
void sort(int *a, int *b, int *c);
int main()
{
int a, b, c;
printf("请输入3个整数:\n");
scanf("%d%d%d", &a, &b, &c);
sort(&a, &b, &c);
printf("从大到小的顺序输出为:%d %d %d\n", a, b, c);
return 0;
}
void sort(int *a, int *b, int *c)
{
int temp;
if (*a < *b)
{
temp = *a;
*a = *b;
*b = temp;
}
if (*a < *c)
{
temp = *a;
*a = *c;
*c = temp;
}
if (*b < *c)
{
temp = *b;
*b = *c;
*c = temp;
}
}
```
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)