算法学习---求数组中的最大数
来源:互联网 发布:哑铃健身软件2015 编辑:程序博客网 时间:2024/06/05 16:42
给定一个数组,取出这个数组中的最大数:
C语言实现:
1: #include <stdio.h>
2:
3: int getMaxNum(int a[],int length)
4: {
5: int max = 0 ;
6: if(length <= 0)
7: {
8: return max ;
9: }
10: for(int i = 0 ; i < length ; i++)
11: {
12: if (max < a[i])
13: {
14: max = a[i] ;
15: }
16: }
17: return max;
18:
19: }
20:
21: int main()
22: {
23: int array[] = {1,2,3,2,5,9,4,11,32,22,10,2};
24: int length = 12 ;
25: printf("Max num is:%d",getMaxNum(array,length)) ;
26: getchar() ;
27: }
Java实现:
1: public class main {
2:
3: public static void main(String[] args) {
4: // TODO Auto-generated method stub
5: int []array = new int[]{1,6,8,2,4,5,9,11,3} ;
6: System.out.println("最大数:" + getMaxNum(array)) ;
7: }
8: public static int getMaxNum(int[] array){
9: int max = 0 ;
10: if(array.length == 0)
11: {
12: return max ;
13: }
14: for(int i = 0 ; i < array.length ;i++)
15: {
16: if(max <= array[i])
17: {
18: max = array[i] ;
19: }
20: }
21: return max ;
22: }
23: }
0 0
- 算法学习---求数组中的最大数
- 递归法求数组中的最大数
- 算法--求数组中的最大和最小值
- 求数组中的最大数和最小数
- 如何求数组中的最大数和第二大数
- 求无序数组中的三个数的最大乘积
- 数组中求最大数
- 分治算法求一个集合中的最大数
- 求数组中的最大子序列的值最优算法
- 求十个数中的最大
- 算法学习二----求子数组的最大和
- 求数组中的最大最小值
- 求四个数中的最大的数
- 求三个数中的最大数(二)
- 求三个数中的最大数
- 求三个数中的最大数
- 求数组中第二最大数
- 分治法求数组最大最小数
- LeetCode[Stack]: Simplify Path
- 关于int *a; int &a; int & *a; int * &a
- HTML5杂
- poj1394——Minimum Inversion Number——————【线段树单点更新、逆序数特性】
- delphi中几种多线程操作方式
- 算法学习---求数组中的最大数
- Codeforces Round #290 (Div. 2)C. Fox And Names(拓扑排序)
- 多线程之ThreadFactory
- (转载)uva题目难度分级列表
- Matlab中实现函数的符号变量
- 动态规划-完全背包(1)
- shell退出,返回小结
- leetcode :twoSum
- filter启动错误,整个web应用也会启动失败。