C++和C语言比较大小题

C++和C语言比较大小题

有3个整数a,b,c,由键盘输入,输出其中最大的数。

C++源代码1:

#include <iostream>
using namespace std;
int main ( )
  {int a,b,c;
   cout<<"please enter three integer numbers:";
   cin>>a>>b>>c;
   if(a<b)
     if(b<c)
       cout<<"max="<<c;
     else
       cout<<"max="<<b;
    else if (a<c)
        cout<<"max="<<c;
    else
        cout<<"max="<<a;
    cout<<endl;
 return 0;
  }

C++源代码2

#include <iostream>
using namespace std;
int main ( )
  {int a,b,c,temp,max ;
   cout<<"please enter three integer numbers:";
   cin>>a>>b>>c;
   temp=(a>b)?a:b;                 /*  将a和b中的大者存入temp中 */
   max=(temp>c)?temp:c;            /*  将a和b中的大者与c比较,最大者存入max */
   cout<<"max="<<max<<endl;
   return 0;
  }

c代码

#include <stdio.h>
int main()
{int a,b,c,max;
 printf("please input a,b,c:\n");
 scanf("%d,%d,%d",&a,&b,&c);
 max=a;
 if (max<b)
   max=b;
 if (max<c)
   max=c;
 printf("The largest number is %d\n",max);
 return 0;
}

输入4个整数,要求按由小到大的顺序输出

C++源代码

#include <iostream>
using namespace std;
int main ()
{int t,a,b,c,d;
 cout<<"enter four numbers:";
 cin>>a>>b>>c>>d;
 cout<<"a="<<a<<", b="<<b<<", c="<<c<<",d="<<d<<endl;
 if (a>b)
 {t=a;a=b;b=t;}
 if (a>c)
  {t=a; a=c; c=t;}
 if (a>d)
  {t=a; a=d; d=t;}
 if (b>c)
  {t=b; b=c; c=t;}
 if (b>d)
  {t=b; b=d; d=t;}
 if (c>d)
   {t=c; c=d; d=t;}
 cout<<"the sorted sequence:"<<endl;
 cout<<a<<", "<<b<<", "<<c<<", "<<d<<endl;
 return 0;
 }

编写一个程序,用来求两个整数或三个整数的最大值


```cpp
#include<iostream>
using namespace std;
int main()
{
int max(int a,int b,int c);
int max(int a,int b);
int a=8,b=-12,c=27;
cout<<"max(a,b,c)="<<max(a,b,c)<<endl;
cout<<"max(a,b)="<<max(a,b)<<endl;
}
int max(int a,int b,int c)
{
    if(b>a)
        a=b;
    if(c>a)
        a=c;
    return a;

}
int max(int a,int b)
{
    if(a>b)
        return a;
    if(b>a)
        return b;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值