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;
}
#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;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值