目录
题目
对3个变量按从小到大顺序排序,要求使用变量的引用。
源码
#include<iostream>
using namespace std;
int main()
{
void sort(int&, int&, int&);
int a, b, c;
cout << "请输入三个变量的值,中间用空格隔开:" << endl;
cin >> a >> b >> c;
sort(a,b,c);
return 0;
}
//交换函数
void swap(int& a,int& b)
{
int temp = a;
a = b;
b = temp;
}
//排序函数
void sort(int& a,int& b,int& c)
{
if (a > b) swap(a, b);
if (a > c) swap(a, c);
if (b > c) swap(b, c);
cout << "3个变量从小到大排序为:" << a <<" "<< b <<" "<< c << endl;
return;
}