codeup之C语言10.1+C语言10.2(指针
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;
}
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;
}