谁帮忙用C++写个程序“读取5个整数并确定和打印其中的最大值”。要能够运行啊
3个回答
2011-03-16
展开全部
#include<iostream>
using namespace std;
void main() //测试用例
{
int values[5], maxval, position;
cout<<"请输入5个整数,以空格隔开:";
cin>>values[0] >> values[1] >>values[2] >>values[3] >>values[4] ;
position = 0;
maxval = values[0];
for (int index = 0; index < 5; index++)
{
if (maxval < values[index])
{
position = index;
maxval = values[index];
}
}
cout<<"最大值为"<<maxval<<" 下标为"<<position<<endl;
system("pause");
}
using namespace std;
void main() //测试用例
{
int values[5], maxval, position;
cout<<"请输入5个整数,以空格隔开:";
cin>>values[0] >> values[1] >>values[2] >>values[3] >>values[4] ;
position = 0;
maxval = values[0];
for (int index = 0; index < 5; index++)
{
if (maxval < values[index])
{
position = index;
maxval = values[index];
}
}
cout<<"最大值为"<<maxval<<" 下标为"<<position<<endl;
system("pause");
}
追问
采纳你了 不过先帮我看看 这是我写的,哪错了,帮忙改改
#include
void main ()
int num[5];
for(int i=0;i>num[i];
}
int max=num[0],mark=0;
for(int i=1;imax) {max=num[i];mark=i;}
}
cout<<"第"<<i+1<<"个数"<<max<<"最大"<<end1;
cout<<end1;
追答
#include
using namespace std; //这一行不能少,否则使用不了cin和cout
void main ()
{
int i, num[5]; //将i定义在这里,你下面2次定义i),编译报错(虽然在for循环中
for(i=0;i>num[i];
}
int max=num[0],mark=0;
for(i=1;imax) {max=num[i];mark=i;}
}
cout<<"第"<<mark<<"个数"<<max<<"最大"<<endl; //注意这里是mark,而不能是i(i在这肯定是6)
cout<<endl;
}
展开全部
#include "stdafx.h"
int get_maxval(int *valarry, int elem_num)
{
if(NULL == valarry)
return -1;
int max = valarry[0];
for(int i=1;i<elem_num;i++)
{
max = (max<valarry[i])?valarry[i]:max;
}
return max;
}
int _tmain(int argc, _TCHAR* argv[])
{
int buf[5] = {0};
//require the user to input the value
for(int i=0;i<5;i++)
{
printf("Please your int type number-NO.%d:\n",i+1);
scanf("%d",buf+i);
}
//get the max one
int max_num = get_maxval(buf, sizeof(buf)/sizeof(int));
//printf the result
printf("The max value is:%d\n",max_num);
return 0;
}
int get_maxval(int *valarry, int elem_num)
{
if(NULL == valarry)
return -1;
int max = valarry[0];
for(int i=1;i<elem_num;i++)
{
max = (max<valarry[i])?valarry[i]:max;
}
return max;
}
int _tmain(int argc, _TCHAR* argv[])
{
int buf[5] = {0};
//require the user to input the value
for(int i=0;i<5;i++)
{
printf("Please your int type number-NO.%d:\n",i+1);
scanf("%d",buf+i);
}
//get the max one
int max_num = get_maxval(buf, sizeof(buf)/sizeof(int));
//printf the result
printf("The max value is:%d\n",max_num);
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
t;a;
cin>>b;
if(a>b) cout<<a<<"is larger"<<endl;
else if(a==b) cout<<"these numbers are equal";
else cout<<b<<"is latrger"<<endl;
2.cout<<"Input three different intergers:";
int a,b,c;
cin>>a;
cin>>b;
cin>>c;
while(a==b||a==c||b==c)
{
cout<<"the numbers must be different,please input again:";
cin>>a;
cin>>b;
cin>>c;
}
cout<<"Sum is "<<a+b+c<<endl;
cout<<"Average is "<<(double)(a+b+c)/(double)3<<endl;
cout<<"Product is "<<a*b*c<<endl;
int sml,lag;
if(a<b)
{
if(a<c)
{
sml=a;
if(b<c) lag=c;
else lag=b;
}
else
}
else{
if(a<c)
else
{
lag=a;
if(b>c) sml=c;
else sml=b;
}
}
cout<<"Smallest is "<<sml<<endl;
cout<<"Lagest is "<<lag<<endl;
3.
*
**
***
****
*****
4.
int num[5];
for(int i=0;i<5;i++)
{cin>>num[i];
}
int max=num[0],mark=0;
for(int i=1;i<5;i++)
{
if(num[i]>max)
}
cout<<"第"<<i+1<<"个数"<<max<<"最大"<<endl;
5.int a;
cin>>a;
if(a%2==0) cout<<"是偶数"<<endl;
else cout<<"是奇数"<<endl;
cin>>b;
if(a>b) cout<<a<<"is larger"<<endl;
else if(a==b) cout<<"these numbers are equal";
else cout<<b<<"is latrger"<<endl;
2.cout<<"Input three different intergers:";
int a,b,c;
cin>>a;
cin>>b;
cin>>c;
while(a==b||a==c||b==c)
{
cout<<"the numbers must be different,please input again:";
cin>>a;
cin>>b;
cin>>c;
}
cout<<"Sum is "<<a+b+c<<endl;
cout<<"Average is "<<(double)(a+b+c)/(double)3<<endl;
cout<<"Product is "<<a*b*c<<endl;
int sml,lag;
if(a<b)
{
if(a<c)
{
sml=a;
if(b<c) lag=c;
else lag=b;
}
else
}
else{
if(a<c)
else
{
lag=a;
if(b>c) sml=c;
else sml=b;
}
}
cout<<"Smallest is "<<sml<<endl;
cout<<"Lagest is "<<lag<<endl;
3.
*
**
***
****
*****
4.
int num[5];
for(int i=0;i<5;i++)
{cin>>num[i];
}
int max=num[0],mark=0;
for(int i=1;i<5;i++)
{
if(num[i]>max)
}
cout<<"第"<<i+1<<"个数"<<max<<"最大"<<endl;
5.int a;
cin>>a;
if(a%2==0) cout<<"是偶数"<<endl;
else cout<<"是奇数"<<endl;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询