同时求最大最小

lqr1992164 2013-01-02 10:11:43
怎样编程,从5个数中取最大和最小,求指教。。。。。。。。。
...全文
373 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
DaiwjDev 2013-01-02
  • 打赏
  • 举报
回复
引用 4 楼 turingo 的回复:
C/C++ code?12345678910111213141516171819202122#include <stdio.h> #define COUNT (5) int main(int argc, char* argv[]){ int a[COUNT] = {12, 8, 9, 20, 10}; int min, max; int i; ……
这才是正解
Mr_Chen__ios专栏 2013-01-02
  • 打赏
  • 举报
回复
#include "stdio.h"
void main()
{
	int i,j,tem,k,a[10];
	int max,min;
	for(i=0;i<10;i++)
	{	
		scanf("%d",&a[i]);
		printf("a[%d]=%d\n",i,a[i]);
	}
	for(i=0;i<=9;i++)
	{
		for(j=0;j<10-i;j++)
		{
			if(a[j]>a[j+1])
			{
			tem=a[j];
			a[j]=a[j+1];
			a[j+1]=tem;
			}
		}
	}
	printf("after sorted \n");
	for(i=1;i<=10;i++)
		printf("%d  ",a[i]);
	min=a[1];
	max=a[10];
	printf("\n");
	printf("最小值%d",a[1]);
	printf("最大值%d",a[10]);

}
此回复必看
图灵狗 2013-01-02
  • 打赏
  • 举报
回复

#include <stdio.h>

#define COUNT	(5)

int main(int argc, char* argv[])
{
	int a[COUNT] = {12, 8, 9, 20, 10};
	int min, max;
	int i;

	max = min = a[0];
	for(i = 1; i < COUNT; i++)
	{
		if(min > a[i])
			min = a[i];
		if(max < a[i])
			max = a[i];
	}
	printf("min=%d, max=%d\n", min, max);

	return 0;
}
DaiwjDev 2013-01-02
  • 打赏
  • 举报
回复

#include <stdio.h>
void main()
{
	int i,j;
	int temp=0;
	int a[5]={3,5,2,6,4};
	for(j=0;j<4;j++){
		for(i=0;i<4;i++){
			if(a[i]>a[i+1]){
				temp=a[i];
				a[i]=a[i+1];
				a[i+1]=temp;
					
			}
		}
	}
	printf("最大值:%d\n最小值:%d\n",a[4],a[0]);
}

东大坡居士 2013-01-02
  • 打赏
  • 举报
回复
GOOLE will give you lots of answers~~~
DaiwjDev 2013-01-02
  • 打赏
  • 举报
回复
冒泡排序过后,去头尾元素啊。
  • 打赏
  • 举报
回复
就是先两两比较就行了,这样的比较次数是1.5N
DaiwjDev 2013-01-02
  • 打赏
  • 举报
回复
引用 9 楼 AnYidan 的回复:
引用 1 楼 Soiol 的回复:冒泡排序过后,去头尾元素啊。 排序,取头尾元素
我的错,手误
Kaile 2013-01-02
  • 打赏
  • 举报
回复
boost::minmax_element 轻松解决 int main() { using namespace std; boost::tuple<int const&, int const&> result1 = boost::minmax(1, 0); assert( result1.get<0>() == 0 ); assert( result1.get<1>() == 1 ); list<int> L; generate_n(front_inserter(L), 1000, rand); typedef list<int>::const_iterator iterator; pair< iterator, iterator > result2 = boost::minmax_element(L.begin(), L.end()); cout << "The smallest element is " << *(result2.first) << endl; cout << "The largest element is " << *(result2.second) << endl; assert( result2.first == std::min_element(L.begin(), L.end()); assert( result2.second == std::max_element(L.begin(), L.end()); }
eRemember 2013-01-02
  • 打赏
  • 举报
回复
引用 8 楼 MoreWindows 的回复:
通常的做法对数组的每个数要操作二次,先与最大的比,然后与最小的比。费时O(2 * N)。 《编程之美》上面讲过一种费时为O(1.5 * N)的解法,代码如下: C/C++ code?123456789101112131415161718192021222324void FindMinMax(int A[],int size,int &min,int &am……
这个好些!
AnYidan 2013-01-02
  • 打赏
  • 举报
回复
引用 1 楼 Soiol 的回复:
冒泡排序过后,去头尾元素啊。
排序,取头尾元素
MoreWindows 2013-01-02
  • 打赏
  • 举报
回复
通常的做法对数组的每个数要操作二次,先与最大的比,然后与最小的比。费时O(2 * N)。 《编程之美》上面讲过一种费时为O(1.5 * N)的解法,代码如下:

void FindMinMax(int A[],int size,int &min,int &max)
{
      max=-INF;
     min=INF;
	for(int i=0;i<size-1;i++)
	{   
		if(A[i]<A[i+1])
		{
          if(A[i+1]>max)
			max=A[i+1];
		if(A[i]<min)
			min=A[i];
		}
		else
		{
         if(A[i]>max)
		max=A[i];
	 	if(A[i+1]<min)
			min=A[i+1];
		}
			
	}
	
}
详细的解释可以看:http://www.myexception.cn/program/797087.html
bigwangdi 2013-01-02
  • 打赏
  • 举报
回复
4楼正解。。

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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