ytu1063——输入三个整数,按由小到大的顺序输出

来源:互联网 发布:海康威视 算法研究院 编辑:程序博客网 时间:2024/05/16 17:23

Description

输入三个整数,按由小到大的顺序输出。分别使用指针和引用方式实现两个排序函数。在主函数中输入和输出数据。

Input

三个整数

Output

由小到大输出成一行,每个数字后面跟一个空格。由指针方式实现。

由小到大输出成一行,每个数字后面跟一个空格。由引用方式实现。

Sample Input

2 3 1

Sample Output

1 2 3 1 2 3

 

代码

#include <iostream>#include <cstring>using namespace std;void sort1(int *a,int *b,int *c){    int t;    if(*a>*b)    {        t=*a;        *a=*b;        *b=t;    }    if(*b>*c)    {        t=*b;        *b=*c;        *c=t;    }    if(*a>*b)    {        t=*a;        *a=*b;        *b=t;    }}void sort2(int &a,int &b,int &c){    int t;    if(a>b)    {        t=a;        a=b;        b=t;    }    if(b>c)    {        t=b;        b=c;        c=t;    }    if(a>b)    {        t=a;        a=b;        b=t;    }}int main(){    void sort1(int *,int *,int *);    void sort2(int &,int &,int &);    int n1,n2,n3;    int *p1,*p2,*p3;    int r1,r2,r3;    cin>>n1>>n2>>n3;    r1=n1;    r2=n2;    r3=n3;    p1=&n1;    p2=&n2;    p3=&n3;    sort1(p1,p2,p3);    cout<<n1<<" "<<n2<<" "<<n3<<endl;    sort2(r1,r2,r3);    cout<<r1<<" "<<r2<<" "<<r3<<endl;    return 0;}


 

 

0 0
原创粉丝点击