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

作者&投稿:苏界 (若有异议请与网页底部的电邮联系)
C语言编程题,从键盘输入任意3个数,按从小到大的顺序输出~

代码1.
// 输入3个数,要求按从小到大顺序输出 #include 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#includeint 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 #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.
"); 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); } }

有两种方法,冒泡法和比较法。
冒泡法牵涉到数组,考虑你是初学者,就不说了,有兴趣自己去看。
比较法:
main(void)//main函数程序的入口
{
int num1,num2,num3,temp;//定义变量存放三个数值和临时变量
printf("please input three numbers");//显示"please input three numbers"
scanf("%d,%d,%d",&num1,&num2,&num3);//输入三个数字
if(num1>num2){temp=num1,num1=num2,num2=temp}//交换数字顺序
if(num2>num3){temp=num2,num2=num3,num3=temp}//交换数字顺序
if(num1>num3){temp=num1,num1=num3,num3=temp}//交换数字顺序
printf("three numbers after sorted: %d,%d,%d/n",num1,num2,num3);//依次输出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
",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语言里怎么从键盘输入一个较大数字,如10^100
用字符串的方法 先用字符串输入一个很大的数 任意 然后一个个转换成整型 附上一部分代码 int num1[1000],num2[1000];main(){ int i=0,count1=0,count2=0,len=0;char a[1000],b[1000];scanf("%s%s",a,b);count1=strlen(a);count2=strlen(b);for(i=count1-1;i>=0;i--)...

c语言数组,怎么样从键盘上输入行数和列数?
你可以这样做:(以int型为例)int **a;int m,n,i;scanf("%d%d",&m,&n); \/\/输入你此次想要创建数组的行数和列数,储存在m和n中 a=(int**)malloc(m*sizeof(int*)); \/*malloc函数在stdlib.h里面,用的时候加入这个头文件*\/for(i=0;i<m;i++)a[i]=(int*)malloc(n*size...

如何用c语言从键盘输入10个数存入数组并求和?
函数中设置一个10个元素的数组以及一个用来求和的变量:2、之后用一个for循环遍历数组,每次都从scanf中取到用户输入的数并存入数组中,存完后再用sum加这个数,如此便能求出数组内元素的和了:3、最后运行程序,输入10个数即可看到结果。以上就是用c语言从键盘输入10个数存入数组并求和的方法:

用C#语言编程,如何从键盘输入一个数组?
键盘输入12,23,34,45,即字符串以逗号分隔,用split方法拆分为数据,帮助中有例子,简单修改即可。建立数组,如只有26个字母,则维数26,循环字符串,用switch判断第几个字母,并在对应的数据中加1,显示时,为0的数组数据不显示,显示的第几维,则字母就是第几个。如果是中文判断,要使用动态数据...

c语言编程如何用键盘输入两位数字输出对应顺序的字母,最后将字母一串显 ...
include <stdio.h>#include <stdlib.h>int main(){ int aa[1000]; int k; printf("请输入你要转换的个数:\\n"); scanf("%d",&k); int p; for(p=0;p<k;p++) { scanf("%d",&aa[p]); } int j; for(j=0;j<k;j++) { printf("...

手机如何更换输入法语言。
e. 点击“添加新键盘”,然后选择要添加的语言。f. 返回到键盘设置页面,找到您刚添加的语言并将其设置为默认键盘。3. Windows Phone系统:a. 打开手机的设置菜单。b. 搜寻并选择“时间和语言”或类似的选项。c. 在语言设置中,选择“添加语言”。d. 选择要添加的语言,然后下载并安装对应的输入法...

C语言如何编写程序从键盘输入一个6位整数,输出该整数的后三位数_百度知...
我直接写核心了:int a[10];for (i=1;i<=6;i++) {scanf("%d",&a[i]);} for (i=4;i<=6;i++) {printf("%d".a[i]);} getch();

c语言怎么从键盘输入数据,用程序怎么编
我用的是vc6,一般格式是:include<iostream> using namespace std;int main(){ cin>>a;return 0;} 在cin>>的后面可以输入数据

如何切换中文输入法?
3、在“键盘和语言”选项卡上, 单击“更改键盘”按钮。4、弹出“文本服务和输入语言”窗口,从“默认输入语言”下拉菜单中选择想要使用的默认输入法,然后一一点击“确定”保存、退出。5、关闭当前打开的窗口。输入法是指为将各种符号输入计算机或其他设备(如手机)而采用的编码方法,同时是书写工具克服...

汇编语言,怎么从键盘输入字符串呢?
DATASSEGMENT A100DUP(?)DATASENDS CODESSEGMENT ASSUMECS:CODES,DS:DATAS,SS:STACKS START:MOVAX,DATAS MOVDS,AX MOVSI,OFFSETA MOVCX,20 L:MOVAH,01H INT21H MOVA[SI],AL INCSI LOOPL MOVAH,4CH INT21H CODESENDS ENDSTART

镜湖区19725229842: C语言如何从键盘输入任意3个数,按从小到大的顺序输出? -
类赖新赛: 代码1. // 输入3个数,要求按从小到大顺序输出#include 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("从小到大...

镜湖区19725229842: c语言编写程序从键盘输入3个整数,按由小到大输出 -
类赖新赛: #include <stdio.h>#define swap(a, b) { t = a; a = b; b = t; } int main() {int a, b, c, t;scanf("%d%d%d", &a,&b,&c);if(a < b) swap(a, b);if(a < c) swap(a, c);if(b < c) swap(b, c);printf("%d %d %d\n", a,b,c); }

镜湖区19725229842: 设计C语言程序,由键盘输入任意三个数,按降序将其输出,,,谢谢啦帮个忙 -
类赖新赛: #include <stdio.h> void main() {int a,b,c,t; printf("请输入三个数:"); scanf("%d%d%d",&a,&b,&c); if(a>b) t=a,a=b,b=t; if(b>c) t=b,b=c,c=t; if(a>b) t=a,a=b,b=t; printf("从小到大顺序为:%d<%d<%d",a,b,c); }

镜湖区19725229842: 设置C语言程序,由键盘输入3个数,按降序将其输出 -
类赖新赛: 按照如下思路即可:1 定义三个元素的数组;2 输入三个数;3 对数组进行降序排序;4 输出数组.代码:int main() { int a[3],i,j,t; for(i = 0; i < 3; i ++) scanf("%d",a+i); for(i = 0; i < 2; i ++) for(j = i+1; j < 3; j++) if(a[i]>a[j]) { t = a[i]; a[i] = a[j]; a[j] = t; } for(i = 0; i < 3; i ++) printf("%d,",a[i]); }

镜湖区19725229842: C语言编程题,从键盘输入任意3个数,按从小到大的顺序输出 -
类赖新赛: #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); }

镜湖区19725229842: 设计C语言流程图,由键盘输入任意3个数,按降序将其输出? -
类赖新赛: #include void main() {int a,b,c,t; printf("请输入三个数:"); scanf("%d%d%d",&a,&b,&c); if(a>b) t=a,a=b,b=t; if(b>c) t=b,b=c,c=t; if(a>b) t=a,a=b,b=t; printf("从小到大顺序为:%d<%d<%d",a,b,c); }

镜湖区19725229842: 用C语言编程 : 从键盘输入3个数,按从小到大输出 -
类赖新赛: 如果只是3个数的话 一:如果是从大到小输出的话 #includevoid 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: %...

镜湖区19725229842: C语言从键盘输入三个数 -
类赖新赛: #include#include int main(){ double a,b,c; scanf("%lf %lf %lf",&a,&b,&c); if(!(a>0&&b>0&&c>0&&a+b>c&&a+c>b&&b+c>a))printf("不能组成三角形"); else { if(a==b&&b==c)printf("等边三角形"); else if(a*a+b*b==c*c||a*a==b*b+c*c||a*a+c*c==b*b)printf("直角三角形"); else printf("一般三角形");} return 0;}

镜湖区19725229842: c语言编程的题 “从键盘输入3个数,使其按从小到大的顺序排列输出.” 感谢各位帮忙! -
类赖新赛: #includevoid sort(int &a,int &b)//对两个数进行排序 { int d; if(a>b) { d=a;a=b;b=d; } } void main() { int a,b,c,d; printf("输入三个数"); scanf("%d%d%d",&a,&b,&c); sort(a,b); sort(a,c); sort(b,c); printf("%d %d %d",a,b,c); }

镜湖区19725229842: 编写C语言程序,实现从键盘输入a,b,c,3个整数,然后按从小到大的顺序输出 -
类赖新赛: 其实很简单啊...用IF语句,Scanf作为键盘接收A,B,C这3个数,T是用来比较大小的. #includemain() { int a,b,c,t; printf("请输入a,b,c这3个数:"); scanf('%d%d%d",&a,&b,&c); printf("a=%d,b=%d,c=%d\n",a,b,c); if(a>b) /*如果A比B...

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网