Description
输入a和b两个整数,按先大后小的顺序输出a和b。注意请使用指针变量的方式进行比较和输出。
Input
两个用空格隔开的整数a和b。
Output
按先大后小的顺序输出a和b,用空格隔开。
请注意行尾输出换行。
Sample Input Copy
5 9
Sample Output Copy
9 5
solution
#include <stdio.h>
void sortPrint(int *a, int *b){
if(*a < *b){
int x = *a;
*a = *b;
*b = x;
}
printf("%d %d", *a, *b);
}
int main(){
int a, b;
int *p = &a, *q = &b;
scanf("%d %d", p, q);
sortPrint(p, q);
return 0;
}
#include <stdio.h>
void sort(int *a, int *b){
if(*a < *b){
int t = *a;
*a = *b;
*b = t;
}
}
int main(){
int a, b;
scanf("%d%d", &a, &b);
sort(&a, &b);
printf("%d %d", a, b);
return 0;
}
Description
输入a、b、c三个整数,按先大后小的顺序输出a、b和c。注意请使用指针变量的方式进行比较和输出。
Input
三个用空格隔开的整数a、b和c。
Output
按先大后小的顺序输出a、b和c,用空格隔开。
请注意行尾输出换行。
Sample Input Copy
9 0 10
Sample Output Copy
10 9 0
solution
#include <stdio.h>
void sortPrint(int *a, int *b, int *c){
if(*a < *b){
int x = *a;
*a = *b;
*b = x;
}
if(*a < *c){
int x = *a;
*a = *c;
*c = x;
}
if(*b < *c){
int x = *c;
*c = *b;
*b = x;
}
printf("%d %d %d", *a, *b, *c);
}
int main(){
int a, b, c;
int *p = &a, *q = &b, *m = &c;
scanf("%d %d %d", p, q, m);
sortPrint(p, q, m);
return 0;
}
该博客内容涉及使用指针变量对输入的整数进行排序。提供了两种不同的C语言解决方案,分别对两个和三个整数进行升序排序,然后按降序输出。程序通过交换指针所指向的值来实现排序,最后打印排序后的结果。

588

被折叠的 条评论
为什么被折叠?



