C语言编程题,从键盘输入任意3个数,按从小到大的顺序输出

如题!!

代码1.

// 输入3个数,要求按从小到大顺序输出  
  
#include <stdio.h>  
  
int main()  
{  
    int a,b,c,t;  
    printf("请输入三个数:");  
    scanf("%d%d%d",&a,&b,&c);  
    if(a > b)  
    {  
        t = a;  
        a = b;  
        b = t;  
    }  
    if(a > c)  
    {  
        t = a;  
        a = c;  
        c = t;  
    }  
    if(b > c)  
    {  
        t = b;  
        b = c;  
        c = t;  
    }  
    printf("从小到大的顺序是:%d  %d  %d\n",a,b,c);  
    return 0;  
}

代码2.

输入3个字符串,按从小到大顺序输出。  //先用程序对三个数进行从小到大排序,然后修改程序
#include<stdio.h>
#include<string.h>
int main()
{void swap(char *pt1,char *pt2);
 char a[20],b[20],c[20];
 char *p1,*p2,*p3;
 printf("请输入三个字符串:");
 gets(a);
 gets(b);
 gets(c);
 //或用scanf("%s,%s,%s",a,b,c);
 p1=&a[0];p2=&b[0];p3=&c[0];//三个指针分别指向三个字符数组
 if(strcmp(*p1,*p2)>0)swap(p1,p2);//if(strcmp(a,b)>0)swap(a,b); //比较两个字符串的大小,为什么用前一句的时候会出现警告呢
 
 if(strcmp(a,c)>0)swap(a,c);//if(strcmp(*p1,*p3)>0)swap(*p1,*p3);
 if(strcmp(b,c)>0)swap(b,c);// if(strcmp(*p2,*p3)>0)swap(*p2,*p3);
 printf("由小到大排列:%s\n%s\n%s\n",a,b,c);
 return 0;
}

void swap(char *pt1,char *pt2)
{ char t[20];
   strcpy(t,pt1);
   strcpy(pt1,pt2);
   strcpy(pt2,t);
//t=*pt1;*pt1=*pt2;*pt2=t;
}

代码3.

#include<stdio.h> 
#include<string.h> 
#define SIZE 3 
#define LEN 50 


int main(void) 

    char str[SIZE][LEN]; 
    char (*pst)[LEN]=str; 
    char temp[LEN]; 
    int i,j; 
    
    printf("Please enter 3 string.\n"); 
    
    for(i=0;i<SIZE;i++) 
    { 
        fgets(*(pst+i),LEN,stdin);
     }
    printf("Befor sort:\n"); 
    
    for(i=0;i<SIZE;i++)
     { 
         fputs(*(pst+i),stdout);
     
     } 
     for(i=0;i<SIZE-1;i++)
          for(j=i+1;j<SIZE;j++) 
          { 
          
              if(strcmp(*(pst+i),*(pst+j)) == 1)
              { 
                  strcpy(temp,*(pst+i));
                  strcpy(*(pst+i),*(pst+j));
                  strcpy(*(pst+j),temp);
               } 
            
           } 
       printf("After sort:\n"); 
       for(i=0;i<SIZE;i++) 
       { 
           fputs(*(pst+i),stdout);
        }
       
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-07-28
#include "stdio.h"
main()
{
int x,y,z,t;
scanf("%d%d%d",&x,&y,&z);
if (x>y)
{
t=x;x=y;y=t;
} /*交换x,y的值*/
if(x>z)
{
t=z;z=x;x=t;
}/*交换x,z的值*/
if(y>z)
{
t=y;y=z;z=t;
}/*交换z,y的值*/
printf("small to big: %d %d %d\n",x,y,z);
}本回答被网友采纳
第2个回答  2019-03-31
#include <stdio.h>

//输入三个数,按从小到大顺序输出 

int main(void)
{
int a, b, c, min, median, max;

scanf("%d%d%d", &a,&b,&c);

if (a>b)
{
if (a>c)
{
max = a;
if (b>c)
{
median = b;
min = c;
}
else
{
median = c;
min = b;
}
}
else
{
max = c;
median = a;
min = b;
}
}
else if (b>c)
{
max = b;
if(a>c)
{
median = a;
min = c;
}
else
{
median = c;
min = a;
}
}

printf("%d < %d < %d\n", min, median, max);

return 0;
}

第3个回答  2013-07-28
#incude<stdio.h>main(){int a,b,c,t;</p><p> scanf("%d,%d,%d",&a,&b,&c);</p><p> if(a>b) {t=a;a=b;b=t;} if(c<a) {t=a;a=c;c=b;b=t;} else if (c<b) {b=c;c=t;}printf("the order of them is:%d<%d<%d",a,b,c);}可以吗?虽然简单,但是容易懂!呵呵。。。
第4个回答  2013-07-28
main()
{
int s[3],i,j,a;
printf("请输入4个整数(各数之间用空格隔开):");
for(i=0;i<3;i++)
{
scanf("%d",&s[i]);
}
for(i=0;i<3;i++)
{
for(j=i;j<3;j++)
{
if(s[j]<s[i])
{
a=s[j];
s[j]=s[i];
s[i]=a;
}
}
}
printf("\n");
for(i=0;i<3;i++)
{
printf("%d ",s[i]);
}
}
相似回答