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

C++求找错(输入三个数,按由大到小顺序打印出来)感谢(初学)

浩宇之轩 发布于 2022-04-05 10:09, 1700 次点击
#include <iostream>
using namespace std;
int main()
{
float a,b,c,t;
cin>>a>>b>>c;
if (a>b);
{
t=a,a=b,b=t;//把a,b中较小的放给a
}
if (a>c);
{
t=a;a=c;c=t;
}//a,c中小的放到a
if(b>c)
{
t=b;b=c;c=a;
}//b,c中小的放到b,大的放到c
cout<<c<<" "<<b<<" "<<a;

return 0;
}
3 回复
#2
rjsp2022-04-05 20:29
if (a>b);
这个后面不应该加分号

程序代码:
#include <iostream>
using namespace std;

int main( void )
{
    float a, b, c;
    cin >> a >> b >> c;

    if( a > b )
    {
        float t = a;
        a = b;
        b = t;
    }

    if( a > c )
    {
        float t = a;
        a = c;
        c = t;
    }

    if( b > c )
    {
        float t = b;
        b = c;
        c = t;
    }

    cout << c << " " << b << " " << a << endl;
}

#3
rjsp2022-04-05 20:33
程序代码:
#include <iostream>
#include <utility>
using namespace std;

int main( void )
{
    float a, b, c;
    cin >> a >> b >> c;

    if( a > b )
        swap( a, b );

    if( a > c )
        swap( a, c );

    if( b > c )
        swap( b, c );

    cout << c << " " << b << " " << a << endl;
}
#4
rjsp2022-04-05 20:43
程序代码:
#include <iostream>
#include <iterator>
#include <algorithm>
using namespace std;

int main( void )
{
    double buf[3];
    copy_n( istream_iterator<double>(cin), 3, buf );

    sort( begin(buf), end(buf), greater<double>() );

    copy( begin(buf), end(buf), ostream_iterator<double>(cout," ") );
}


[此贴子已经被作者于2022-4-5 20:45编辑过]

1