3580 - 指针练习

通过次数

0

提交次数

0

时间限制 : 1 秒 内存限制 : 128 MB

输入两个整数,按从小到大打印出来。注意: 必须使用指针

题目输入

题目输出

输入/输出样例

输入格式

5 3

输出格式

3 5

C语言解答

#include<stdio.h>
#include<stdlib.h>

void sort(int *a,int *b)
{
	int tmp;
	if(*a>*b)
	{
		tmp=*a;
		*a=*b;
		*b=tmp;
	}
}
int main()
{
	int a,b;
	scanf("%d%d",&a,&b);
	sort(&a,&b);
	printf("%d %d",a,b);
	return 0;
}

C++解答

#include<iostream>
#include<cstdio>

using namespace std;
int swap(int x,int y)
{
	int t;
	t=x;
	x=y;
	y=t;
}
int main()
{
	int *point1,*point2,a,b;
	
	cin>>a>>b;
	
	point1=&a;
	point2=&b;
	
	if(a>b)
	swap(point1,point2);
	cout<<*point1<<" "<<*point2;
	
	return 0;
}