C语言如何从键盘输入任意3个数,按从小到大的顺序输出? 求C语言编程,要求输入任意三个数,按从小到大的顺序输出。

C\u8bed\u8a00\u7f16\u7a0b\u9898,\u4ece\u952e\u76d8\u8f93\u5165\u4efb\u610f3\u4e2a\u6570,\u6309\u4ece\u5c0f\u5230\u5927\u7684\u987a\u5e8f\u8f93\u51fa

\u4ee3\u78011.
// \u8f93\u51653\u4e2a\u6570\uff0c\u8981\u6c42\u6309\u4ece\u5c0f\u5230\u5927\u987a\u5e8f\u8f93\u51fa #include int main() { int a,b,c,t; printf("\u8bf7\u8f93\u5165\u4e09\u4e2a\u6570\uff1a"); 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("\u4ece\u5c0f\u5230\u5927\u7684\u987a\u5e8f\u662f\uff1a%d %d %d\n",a,b,c); return 0; }\u4ee3\u78012.
\u8f93\u51653\u4e2a\u5b57\u7b26\u4e32\uff0c\u6309\u4ece\u5c0f\u5230\u5927\u987a\u5e8f\u8f93\u51fa\u3002 //\u5148\u7528\u7a0b\u5e8f\u5bf9\u4e09\u4e2a\u6570\u8fdb\u884c\u4ece\u5c0f\u5230\u5927\u6392\u5e8f\uff0c\u7136\u540e\u4fee\u6539\u7a0b\u5e8f#include#includeint main(){void swap(char *pt1,char *pt2); char a[20],b[20],c[20]; char *p1,*p2,*p3; printf("\u8bf7\u8f93\u5165\u4e09\u4e2a\u5b57\u7b26\u4e32:"); gets(a); gets(b); gets(c); //\u6216\u7528scanf("%s,%s,%s",a,b,c); p1=&a[0];p2=&b[0];p3=&c[0];//\u4e09\u4e2a\u6307\u9488\u5206\u522b\u6307\u5411\u4e09\u4e2a\u5b57\u7b26\u6570\u7ec4 if(strcmp(*p1,*p2)>0)swap(p1,p2);//if(strcmp(a,b)>0)swap(a,b); //\u6bd4\u8f83\u4e24\u4e2a\u5b57\u7b26\u4e32\u7684\u5927\u5c0f\uff0c\u4e3a\u4ec0\u4e48\u7528\u524d\u4e00\u53e5\u7684\u65f6\u5019\u4f1a\u51fa\u73b0\u8b66\u544a\u5462 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("\u7531\u5c0f\u5230\u5927\u6392\u5217\uff1a%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;}\u4ee3\u78013.
#include #include #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); } }

\u6709\u4e24\u79cd\u65b9\u6cd5\uff0c\u5192\u6ce1\u6cd5\u548c\u6bd4\u8f83\u6cd5\u3002
\u5192\u6ce1\u6cd5\u7275\u6d89\u5230\u6570\u7ec4\uff0c\u8003\u8651\u4f60\u662f\u521d\u5b66\u8005\uff0c\u5c31\u4e0d\u8bf4\u4e86\uff0c\u6709\u5174\u8da3\u81ea\u5df1\u53bb\u770b\u3002
\u6bd4\u8f83\u6cd5\uff1a
main(void)//main\u51fd\u6570\u7a0b\u5e8f\u7684\u5165\u53e3
{
int num1,num2,num3,temp;//\u5b9a\u4e49\u53d8\u91cf\u5b58\u653e\u4e09\u4e2a\u6570\u503c\u548c\u4e34\u65f6\u53d8\u91cf
printf("please input three numbers");//\u663e\u793a"please input three numbers"
scanf("%d,%d,%d",&num1,&num2,&num3);//\u8f93\u5165\u4e09\u4e2a\u6570\u5b57
if(num1>num2){temp=num1,num1=num2,num2=temp}//\u4ea4\u6362\u6570\u5b57\u987a\u5e8f
if(num2>num3){temp=num2,num2=num3,num3=temp}//\u4ea4\u6362\u6570\u5b57\u987a\u5e8f
if(num1>num3){temp=num1,num1=num3,num3=temp}//\u4ea4\u6362\u6570\u5b57\u987a\u5e8f
printf("three numbers after sorted: %d,%d,%d/n",num1,num2,num3);//\u4f9d\u6b21\u8f93\u51fa3\u4e2a\u6570
}
\u5e0c\u671b\u6211\u7684\u89e3\u7b54\u8ba9\u4f60\u6ee1\u610f\u3002

代码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
",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
%s
%s
",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.
"); 
    
    for(i=0;i<SIZE;i++) 
    { 
        fgets(*(pst+i),LEN,stdin);
     }
    printf("Befor sort:
"); 
    
    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:
"); 
       for(i=0;i<SIZE;i++) 
       { 
           fputs(*(pst+i),stdout);
        }
       
}


代码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);

  • 缂栫▼C璇█绋嬪簭,瀹炵幇濡備笅鍔熻兘:浠庨敭鐩樹换鎰忚緭鍏3涓暣鏁,姹傚嚭3涓暟涓殑
    绛旓細include <iostream> using namespace std;int main(){ int a,b,c;int max;cout<<"璇杈撳叆涓変釜鏁";cin>>a>>b>>c;if(a>b){ max = b;if(c > max)max = c;} else { max = a;if(c > max)max = c;} cout<<"鏈澶у兼槸"<<c<<endl;return 0;} ...
  • 鐢C璇█鍐欏嚭鈥浠庨敭鐩涓浠绘剰杈撳叆3涓鏁存暟,鐒跺悗姣旇緝杩涓変釜鏁鐨勫ぇ灏,浠ュ皬...
    绛旓細include<stdio.h> int main(){ int a,b,c;scanf("%d %d %d",&a,&b,&c);if(a>=b&&b>=c)printf("%8d%8d%8d\n",c,b,a);else if(b>=c&&c>=a)printf("%8d%8d%8d\n",a,c,b);else if(a>=c&&c>=b)printf("%8d%8d%8d\n",b,c,a);else if(b>=a&&a>=c)...
  • 浠庨敭鐩涓杈撳叆浠绘剰涓変釜鏁存暟,璁$畻骞惰緭鍑涓変釜鏁鐨勫钩鍧囧?
    绛旓細C鍙互杩欎箞鍐欌斺斾唬鐮佹枃鏈細include "stdio.h"int main(int argc,char *argv[]){ int x,y,z;printf("Please enter 3 integers...\n");scanf("%d%d%d",&x,&y,&z);printf("They are the average of %g\n",(x+y+z)/3.0);return 0;} ...
  • C璇█绋嬪簭:浠庨敭鐩樿緭鍏ヤ笁涓鏁存暟,鎸夌収浠庡皬鍒板ぇ杈撳嚭,鏉′欢杩愮畻绗﹀疄鐜癬鐧 ...
    绛旓細int a,b,c;int min,max,between;min = (a < b)?((a < c)?a:c):((b < c)?b:c);max = (a > b)?((a>c)?a:c):((b>c)?b:c);绀轰緥濡備笅锛歩nclude<stdio.h> void main(){ int a,b,c;int min,max,between;printf("杈撳叆涓変釜鏁存暟:");scanf("%d%d%d",&a,&b,&c)...
  • C璇█浠鍏ラ棬131閿洏閫氳繃浠绘剰3涓暟
    绛旓細include "stdio.h"int main(){int a,b,c,max;scanf("%d%d%d",&a,&b,&c);max=a>b?a:b;if(c>max)max=c;printf("max=%d\n",max);return 0;}
  • 璁剧疆C璇█绋嬪簭,鐢遍敭鐩樿緭鍏3涓暟,鎸夊崌搴忓皢鍏惰緭鍑
    绛旓細include <stdio.h> void main(){ int i,j锛宼emp;int a[3]={0};printf("杈撳叆3涓暟\n");scanf("%d%d%d",&a[0],&a[1],&a[2]);for (i=0;i<2;i++){ for (j=i+1;j<3;j++){ if (a[i]>a[j]){ temp=a[i];a[i]=a[j];a[j]=temp;} } } for (i=0;i<3...
  • C璇█缂栫▼ 浠庨敭鐩樿緭鍏ヤ笁涓暟鏀惧叆涓缁存暟缁刟rray[3]涓,鎸変粠灏忓埌澶ц緭鍑...
    绛旓細include <stdio.h>void main(){ int i, array[3],a,b,c,t; for(i=0;i<3;i++) //杩欓噷3涓暟锛0銆1銆2鍗冲彲锛屼笉瑕佺瓑浜3 scanf("%d",&array[i]); a=array[0]; b=array[1]; c=array[2]; if(a>b) {t=a;a=b;b=t;} if(a>c) {t=a;a=c...
  • 涓閬C璇█棰樼洰 鏈3涓鏁存暟a,b,c,鐢遍敭鐩樿緭鍏,杈撳嚭鍏朵腑鏈澶х殑鏁,璇风紪鍐...
    绛旓細鎵浠ユ槸 int main (void){ int a,b,c,max;printf("璇杈撳叆涓変釜浠绘剰鏁存暟锛歕n");scanf("%d%d%d",&a, &b, &c);if(a>=b) max=a;else max=b;if(b>=c) max=b;else max=c;if(c>=a) max=c;else max=a;printf("max=%d\n",max);return 0;} 鎴栬呯敤 include<stdio.h>...
  • 濡備綍鐢╟璇█缂栧啓浠庨敭鐩樿緭鍏3涓暟缁檃,b,c,鐒跺悗渚濇浜ゆ崲浠栦滑涓殑鏁?
    绛旓細澶氱湅鍑犻亶鏁扮粍鐨勬帓搴忥紙姣斿鍐掓场鎺掑簭锛夛紝鎸囬拡鐨勪娇鐢紝杩欓亾棰樺氨鑳藉仛鍑烘潵浜嗭紒鏈澶氳姳浣犱袱澶╃殑鏃堕棿锛 鎻愮ず浣犱竴涓嬪惂锛宨nt a,b,c; int *p1,*p2,*p3;杩欓亾棰樹富瑕佽冪殑鍏跺疄鏄寚閽堬紝浣犵殑閭g缂栧啓澶病鎶鏈惈閲忎簡锛
  • 鐢–璇█缂栫▼,闂鏄:鐢遍敭鐩樿緭鍏3涓鏁存暟a,b,c鐒跺悗鎸夌敱灏忓埌澶ч『搴忚緭鍑...
    绛旓細include <stdio.h> void main(){ int a,b,c,max,min,mid;scanf("%d",&a);scanf("%d",&b);scanf("%d",&c);if(a<b)min=a;else min=b;if(min<c)min=c;if(a>b)max=a;else max=b;if(max<c)max=c;if(a!=max&&a!=min)mid=a;if(b!=max&&b!=min)mid=b;if(c!=...
  • 扩展阅读:键盘的1234被锁了 ... 键盘右边1-9打不出数字 ... c++编程 ... 数字0-9键盘盲打指法 ... 三分钟教你快速打字 ... 电脑键盘盲打速记口诀 ... c++连续输入多个整数 ... c 输入 ... c语言基本输入与输出 ...

    本站交流只代表网友个人观点,与本站立场无关
    欢迎反馈与建议,请联系电邮
    2024© 车视网