注册 登录
编程论坛 C++教室

刚学c++,有个题不会了,题很简单,求求来看看

yonglewang 发布于 2008-10-31 19:47, 2268 次点击
有3个数a、b、c,要求按大小顺序把他们输出。
怎么做呀,教教我
在线等
23 回复
#2
jiyongjie2008-10-31 20:37
int temp;
int a[]={1,2,3};
for (int i=0;i<2;i++)
   for(int j=i+1;j<3;j++){
      if (a[j]<a[i])
          temp=a[j];
          a[j]=a[i];
          a[i]=temp;
   }
不知道这样行吗?
#3
choco10242008-10-31 21:01
#include "iostream"
using namespace std;
int main()
{
    int a,b,c,temp;
    cout<<"Please input three numbers a,b,c:";
    cin>>a>>b>>c;
    if(a<b){temp=a;a=b;b=temp;}
    if(a<c){temp=a;a=c;c=temp;}
    if(b<c){temp=b;b=c;c=temp;}
    cout<<a<<"\t"<<b<<"\t"<<c<<endl;
    return 0;
}
#4
斗熊2008-10-31 23:01
好厉害啊呵呵
#5
蜗牛人生2008-10-31 23:30
支持3楼的
#6
shmilytong2008-11-01 01:40
由于不知道a、b、c三个数的类型,而且楼主又是刚学C++,我倒是提议借助这个题图让楼主学会使用函数模板,我的程序如下:
#include <iostream>
using namespace std;
template <typename T>
T max(T a,T b,T c)
  {T temp,d;
    if(a<b){temp=a;a=b;b=temp;}
    if(a<c){temp=a;a=c;c=temp;}
    if(b<c){temp=b;b=c;c=temp;}
    cout<<a<<" "<<b<<" "<<c<<endl;
  }
int main()
  {int a,b,c,d;
   cout<<"Please input three numbers:"<<endl;
   cin>>a>>b>>c;
   d=max(a,b,c);
   cout<<d<<endl;
   return 0;
  }

我也是刚开始学习C++不久
#7
lovely061232008-11-02 18:41
楼上有些错误`我改了下``
#include <iostream>
using namespace std;
template <typename T>
T max(T a,T b,T c)
{
    T temp;
    if(a<b){temp=a;a=b;b=temp;}
    if(a<c){temp=a;a=c;c=temp;}
    if(b<c){temp=b;b=c;c=temp;}
    cout<<a<<">"<<b<<">"<<c<<endl;
    return 0;
}
int main()
{
    int a,b,c;
    cout<<"请输入3个数字按从大到小输出."<<endl;
    cin>>a>>b>>c;
    max(a,b,c);
    return 0;
}
#8
hbcb002008-11-05 11:41
楼上这好像在VC6。0里不行阿`


用DEV才行,我晕哦``搞不懂`
#9
gaohao122008-11-05 17:34
#include<iostream.h>
void main()
{
    float a[3],temp;
         int j=0;
    
    cout<<"请输入三个数:"<<'\n';
    for(int k=0;k<3;k++)
        cin>>a[k];
    
    for(int 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;
            } //比较数组中三个数的大小,并按从小到大排列
            
        }
        cout<<"从小到大的排列顺序是:";
        for(int m=2;m<3&&m>=0;m--)
            cout<<a[m]<<'\t';
        
}
#10
haosw862008-11-05 23:30
试下我这个
#include <iostream>
using namespace std;
template <typename T>
T max(T a,T b,T c)
  {T temp;
    if(a<b){temp=a;a=b;b=temp;}
    if(a<c){temp=a;a=c;c=temp;}
    if(b<c){temp=b;b=c;c=temp;}
    cout<<a<<" "<<b<<" "<<c<<endl;
    return 0;
  }
 int main()
  {float a,b,c,d;
   cout<<"Please input three numbers:"<<endl;
   cin>>a>>b>>c;
   d=max(a,b,c);
   return 0;
  }
#11
ronaldowsy2008-11-06 02:19
你们太喜剧了,连摸版都给别人写出来了,版主连3个数排大小都不会,你给他写那么麻烦的摸班他能懂吗!我还是同意前面比较简单的问题.当开始学习,从简单的开始!
#12
xiaxiang19862008-11-07 11:17
cin>>a>>b>>c;
int max;
max=a>(b>c?b:c)?a:(b>c?b:c);
#13
beyondlwm2008-11-07 11:48
为什么不用冒泡法?
#14
mineral__water2008-11-07 14:28
我是菜鸟!
我是菜鸟!谢谢!
#15
风动2008-11-07 16:05
建议看下冒泡排序,再多几个数也一样可以搞定!
#16
andyxmj2008-11-09 12:16
用模板怎样实现三个不同类型的数的比较?请教?
#17
秦之王翦2008-11-13 20:56
偶也是才学C++的,而这个题目是唐浩强编写的C语言第三版开头几章中的一个习题,只不过学到条件语句而已。用模板楼主咋个看的懂啊????
这是偶写的,楼主看看可以吗?
#include"stdio.h"
void main()
{
    int a,b,c,d;
    scanf("%d %d %d",&a,&b,&c);
    if (a>b) d=a;
    else d=b;
    if (c>d)
    printf("%d\n",c);
    else
    printf("%d\n",d);
}
#18
flamemiracle2008-11-13 22:14
简单问题搞复杂了哦
#19
yangdongsen2008-11-14 09:07
上面那位说的应该是谭浩强吧
#20
uo314872008-11-14 12:46
看看我这个,我也刚学。。
#include<iostream.h>
int main()
{
int a,b,c ;
    cout<<"请输入3个数字:"<<endl;
    cin>>a>>b>>c;
    if(a<b)
    {
        int temp;
        temp=a;
        a=b;
        b=temp;
    }
    if(a<c)
    {
        int temp;
        temp=a;
        a=c;
        c=temp;
    }
    if(b<c)
    {
        int temp;
        temp=b;
        b=c;
        c=temp;
    }


    cout<<a<<" "<<b<<" "<<c<<endl;
    return 0;
}
#21
uo314872008-11-14 12:48
如果正确一定要回复我个啊。
#22
RexChin2008-11-14 15:18
越高越复杂哦~
#23
jymengxue2008-11-14 19:26
你们的函数模板有什么用啊。。。。。。
#24
发呆12008-11-15 09:33
函数模板不错,呵呵,尽管有点麻烦,可是,练练手也是不错的。
1