输入三个字符串,按由小到大的顺序输出
时间: 1ms 内存:128M
描述:
输入三个字符串,按由小到大的顺序输出。分别使用指针和引用方式实现两个排序函数。在主函数中输入和输出数据。
输入:
3行字符串
输出:
按照从小到大输出成3行。由指针方式实现。按照从小到大输出成3行。由引用方式实现。
示例输入:
cde
afg
abc
示例输出:
abc
afg
cde
abc
afg
cde
提示:
参考答案(内存最优[752]):
#include<stdio.h>
#include<string.h>
void Swap(char **q1, char **q2)
{
char *t;
t= *q1;
*q1 = *q2;
*q2 = t;
}
int main()
{
int i=0;
char *p1,*p2,*p3, str1[80],str2[80],str3[80];
p1=str1;
p2=str2;
p3=str3;
gets(str1);
gets(str2);
gets(str3);
if (strcmp(p1, p2)>0)
Swap(&p1, &p2);
if (strcmp(p1, p3)>0)
Swap(&p1, &p3);
if (strcmp(p2, p3)>0)
Swap(&p2, &p3);
printf("%s\n%s\n%s\n",p1,p2,p3);
return(0);
}
参考答案(时间最优[0]):
#include<stdio.h>
#include<string.h>
void Swap(char **q1, char**q2);
int main()
{
int i=0;
char *p1,*p2,*p3, str1[80],str2[80],str3[80];
p1=str1; p2=str2; p3=str3;
gets(str1);
gets(str2);
gets(str3);
if (strcmp(p1, p2)>0)
Swap(&p1, &p2);
if (strcmp(p1, p3)>0)
Swap(&p1, &p3);
if (strcmp(p2, p3)>0)
Swap(&p2, &p3);
printf("%s\n%s\n%s\n",p1,p2,p3);
return(0);
}
void Swap(char **q1, char* *q2)
{char *t;
t= *q1;
*q1 = *q2;
*q2 = t;
}
题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。