输入三个整数,按由小到大的顺序输出
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 669 Solved: 302
[Submit][Status][Web Board]
Description
输入三个整数,按由小到大的顺序输出。分别使用指针和引用方式实现两个排序函数。在主函数中输入和输出数据。
Input
三个整数
Output
由小到大输出成一行,每个数字后面跟一个空格。由指针方式实现。
由小到大输出成一行,每个数字后面跟一个空格。由引用方式实现。
Sample Input
2 3 1
Sample Output
1 2 3 1 2 3
HINT
主函数已给定如下,提交时不需要包含下述主函数
/* 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> #include<string> using namespace std; int main() { void sort1(char *,char *,char *); void sort2(string &,string &,string &); char s1[100],s2[100],s3[100]; char *p1,*p2,*p3; string r1,r2,r3; cin>>s1>>s2>>s3; r1=string(s1); r2=string(s2); r3=string(s3); p1=s1; p2=s2; p3=s3; sort1(p1,p2,p3); cout<<s1<<endl<<s2<<endl<<s3<<endl; sort2(r1,r2,r3); cout<<r1<<endl<<r2<<endl<<r3<<endl; return 0; } void sort1(char *a,char *b,char *c) {char p[100]; if(strcmp(a,b)>0) {strcpy(p,a); strcpy(a,b); strcpy(b,p);} if(strcmp(b,c)>0) { strcpy(p,b); strcpy(b,c); strcpy(c,p);} if(strcmp(a,b)>0) {strcpy(p,a); strcpy(a,b); strcpy(b,p);} } void sort2(string &a,string &b,string &c) {string n; if(a>b) {n=a; a=b; b=n;} if(b>c) {n=b; b=c; c=n; } if(a>b) {n=a; a=b; b=n;} }