zoukankan      html  css  js  c++  java
  • 2.任意输入三个数,求最大数

    (1)笨办法,采用if嵌套和&&判断,比较消耗资源,不过也能达到要求:
     
    #include<iostream>
    
    using namespace std;
    
    int main()
    {
        int a,b,c,max;
        cout<<"please input 3 numbers:"<<endl;
        cin>>a>>b>>c;
        if(a>b&&a>c)
            max=a;
        else
            if(b>c&&b>a)
            max=b;
        else
            max=c;
    
        cout<<a<<" "<<b<<" "<<c<<" "<<"三个数中,最大的是:"<<max<<endl;
    
        return 0;
     }
    (2)采用三目运算符,程序变得简便许多:
     
    #include<iostream>
    
    using namespace std;
    
    int main()
    {
        int a,b,c,max;
        cout<<"please input 3 numbers:"<<endl;
        cin>>a>>b>>c;
        max=(a>b)?a:b;
        if(c>max)
            max=c;
    
        cout<<a<<" "<<b<<" "<<c<<" "<<"最大的数为: "<<max<<endl;
    
        return 0;
     }
    (3)调用一个函数:
     
    #include<iostream>
    using namespace std;
    int maxNum(int,int,int);
    
    int main()
    {
        int a,b,c,max;
        cout<<"please input 3 numbers:"<<endl;
        cin>>a>>b>>c;
        max=maxNum(a,b,c);//调用函数
        cout<<a<<" "<<b<<" "<<c<<" "<<"最大的数为: "<<max<<endl;
        return 0;
     }
    
    int maxNum(int a,int b,int c)//不要忘记参数定义
    {
        if(a>=b&&a>=c)
            return a;
        else
            if(b>=a&&b>=c)
            return b;
        else
            return c;
    }
     
    //int numMax(int x,int y,int z)
    //{
    //    int max;
    //    //max=(x>y)?x:y;
    //    //if(z>max)
    //    //    max=z;
    //    //return max;
    //    if(x>=y&&x>=z)
    //    {
    //        max=x;
    //    }else if(y>=x&&y>=z)
    //    {
    //        max=y;
    //    }else
    //    {
    //        max=z;
    //    }
    //    return max;
    //}

  • 相关阅读:
    [CF1042F]Leaf Sets
    [CF1051F]The Shortest Statement
    [洛谷P1792][国家集训队]种树
    [CF484E]Sign on Fence
    [洛谷P2216][HAOI2007]理想的正方形
    [洛谷P4389]付公主的背包
    [洛谷P4726]【模板】多项式指数函数
    服务器上Ubuntu系统安装
    删除ubuntu系统
    Win10下安装Ubuntu16.04双系统
  • 原文地址:https://www.cnblogs.com/jixiaowu/p/3890237.html
Copyright © 2011-2022 走看看