求助??

xiaoqiang97 2009-02-21 02:44:01
如何输出数组中的最大值。新手,希望详细点。谢谢
...全文
98 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
harry330 2009-02-22
  • 打赏
  • 举报
回复
将数据放到vector(数组也可)中(假设变量名为arr),然后利用STL中的排序函数即可。

std::nth_element(arr.begin(), arr.begin()+ 1, arr.end());
然后arr中的第一个元素为最大。
send068 2009-02-21
  • 打赏
  • 举报
回复
友情up
netsocket 2009-02-21
  • 打赏
  • 举报
回复
友情up
szqh97 2009-02-21
  • 打赏
  • 举报
回复
直接遍历数组输出最大值就可以了
hackers007 2009-02-21
  • 打赏
  • 举报
回复
int max(int *a,int len)
{
int tmp=a[0];
for(int index=1;index<len;index++){
if(tmp<a[index]) tmp=a[index];
}
return tmp;
}
yangch_nhcmo 2009-02-21
  • 打赏
  • 举报
回复
直接遍历数组就OK了
fairchild811 2009-02-21
  • 打赏
  • 举报
回复
5楼的泛型算法简单。
bfhtian 2009-02-21
  • 打赏
  • 举报
回复
1楼就满足要求了
savvyzheng 2009-02-21
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 a_rockboy 的回复:]
int a[] = { 2, 4, 3, 6, 7};
int max = a[0];
for(int i = 1; i < sizeof(a)/ sizeof(a[0]); i ++)
{
if(max < a[i])
max = a[i];
}

cout < < max < < endl;
[/Quote]

这样就可以了,方法很多,数据结构的东西,自己想想就行了
tangshuiling 2009-02-21
  • 打赏
  • 举报
回复
int a[10]={1233,4758,8219,347,1273,672,90,200,100,8888};
cout<<*max_element(a,a+sizeof(a)/sizeof(int));
  • 打赏
  • 举报
回复
不派序了,直接把数组遍历一遍,输出最大的.

#include <stdio.h>
#define MAX_LEN 10
void main()
{
int i,max;
int a[MAX_LEN]={1233,4758,8219,347,1273,672,90,200,100,8888};
max=a[0];
for(i=1;i<MAX_LEN;i++)
if(max<a[i])
max=a[i];
printf("%d\n",max);
}

operatingtuzi 2009-02-21
  • 打赏
  • 举报
回复
一楼的就可以哦

这个一般都要n阶的吧
waizqfor 2009-02-21
  • 打赏
  • 举报
回复
[Quote=引用楼主 xiaoqiang97 的帖子:]
如何输出数组中的最大值。新手,希望详细点。谢谢
[/Quote]
直接交换排序

#include <stdio.h>
void main()
{
int a[10]={1233,4758,8219,347,1273,672,90,200,100,10},i,j,t=0;
for(i=0;i<10;i++)
for(j=i+1;j<10;j++)
{
if(a[i]>a[j])
{t=a[i];a[i]=a[j];a[j]=t;}
}
for(i=0;i<10;i++)
printf("%d\n",a[i]);
}
a_rockboy 2009-02-21
  • 打赏
  • 举报
回复
int a[] = { 2, 4, 3, 6, 7};
int max = a[0];
for(int i = 1; i < sizeof(a)/ sizeof(a[0]); i ++)
{
if(max < a[i])
max = a[i];
}

cout << max << endl;

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧