求十个数中最大的数

十个数求最大数,简化为两个数进行九次大小比较。

//使用 c 语言编写程序,求 10 个整数中的最大值。
#include<stdlib.h>
typedef int ElemType;
ElemType Judgement_tow_num_size();//返回两个数中最大的数
int main()
{
	ElemType a[10] = { 12, 23, 34, 45, 56, 67, 78, 89, 90, 100 };
	ElemType t=a[0];
	for (int i = 0; i<10; i++)
	{
		t =Judgement_tow_num_size(t,a[i+1]);
	}
	printf("%d\n", t);
	return 0;
}
ElemType Judgement_tow_num_size1(ElemType x, ElemType y)
{
	return x > y ? x : y;
}

仔细想了一下,当给出更多数时,运算是否可以更快一点,于是自己便想出了如下代码:

typedef int ElemType;
ElemType Judgement_tow_num_size();//返回两个数中最大的数
int main()
{
	ElemType a[10] = { 12, 23, 34, 45, 56, 67, 78, 89, 90, 100 };
	ElemType t=a[0],k=a[9];
	for (int i = 0; i<10/2; i++)
	{
		t =Judgement_tow_num_size(t,a[i+1]);
		k = Judgement_tow_num_size(k, a[9 - i]);
	}
	t = Judgement_tow_num_size(t, k);
	printf("%d\n", t);
	return 0;
}
ElemType Judgement_tow_num_size(ElemType x, ElemType y)
{
	return x > y ? x : y;
}

从两头分别求最大值,即分作两部分,分别求最大值,最后将两个最大值比较,即求得总体的最大数。
注:两个数的大小比较Judgement_tow_num_size1(ElemType x, ElemType y)函数的具体实现可阅读博主的c语言求两个数的中较大的一个

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值