编写一个c++程序:输入3个整数a、b、c,按从大到小的顺序输出。要求通过指针实现
时间: 2024-02-24 10:25:02 浏览: 25
```
#include <stdio.h>
void sort(int *a, int *b, int *c);
int main() {
int a, b, c;
printf("请输入三个整数:\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;
}
}
```
相关问题
c++编写:输入3个整数a、b、c,按从大到小的顺序输出。要求通过指针实现
这是一个比较简单的编程问题,可以通过以下代码实现:
```c
#include <stdio.h>
void sort_desc(int *a, int *b, int *c);
int main() {
int a, b, c;
printf("请输入3个整数:");
scanf("%d %d %d", &a, &b, &c);
sort_desc(&a, &b, &c);
printf("按照从大到小的顺序输出:%d %d %d", a, b, c);
return 0;
}
void sort_desc(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;
}
}
```
上面的代码是通过指针实现从大到小排序的,可以输入任意三个整数,并将它们按照从大到小的顺序进行输出。
用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", a, b, c);
return 0;
}
void sort(int *a, int *b, int *c) {
int tmp;
if (*a < *b) {
tmp = *a;
*a = *b;
*b = tmp;
}
if (*a < *c) {
tmp = *a;
*a = *c;
*c = tmp;
}
if (*b < *c) {
tmp = *b;
*b = *c;
*c = tmp;
}
}
```