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;
}