YTU OJ1063: 輸入三個整數,按由小到大的順序輸出
阿新 • • 發佈:2018-12-14
1063: 輸入三個整數,按由小到大的順序輸出
時間限制: 1 Sec 記憶體限制: 128 MB 提交: 1848 解決: 915 [提交][狀態][討論版][命題人:外部匯入]
題目描述
輸入三個整數,按由小到大的順序輸出。分別使用指標和引用方式實現兩個排序函式。在主函式中輸入和輸出資料。
輸入
三個整數
輸出
由小到大輸出成一行,每個數字後面跟一個空格。由指標方式實現。
由小到大輸出成一行,每個數字後面跟一個空格。由引用方式實現。
樣例輸入
2 3 1
樣例輸出
1 2 3 1 2 3
提示
主函式已給定如下,提交時不需要包含下述主函式
/* C++程式碼 */
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;
}
來源
#include<iostream> using namespace std; void sort1(int *a,int *b,int *c) { int temp; if(*a>*b) temp=*a,*a=*b,*b=temp; if(*a>*c) temp=*a,*a=*c,*c=temp; if(*b>*c) temp=*b,*b=*c,*c=temp; } void sort2(int &a,int &b,int &c) { int temp; if(a>b) temp=a,a=b,b=temp; if(a>c) temp=a,a=c,c=temp; if(b>c) temp=b,b=c,c=temp; } 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; }