逻辑:比较两数大小,交换位置使数值小的数排在前面。
写法一(交换指针指向的地址中存放的数据):
#include <iostream>
using namespace std;
int main()
{
int a,b,c;
int *p1,*p2,*p3;
int temp;
cout<<"输入3个整数:"<<endl;
cin>>a>>b>>c;
p1 = &a;
p2 = &b;
p3 = &c;
if(*p1 > *p2)
{
temp = *p1;
*p1 = *p2;
*p2 = temp;
}
if(*p1 > *p3)
{
temp = *p1;
*p1 = *p3;
*p3 = temp;
}
if(*p2 > *p3)
{
temp = *p2;
*p2 = *p3;
*p3 = temp;
}
cout<<'\n'
<<"按由小到大顺序输出:"<<'\n'
<<a<<'\n'
<<b<<'\n'
<<c<<'\n';
return 0;
}
写法二(交换指针指向的地址):