找出一个整数数组中,第二大的数
来源:互联网 发布:龙蛇演义 知乎 编辑:程序博客网 时间:2024/05/08 16:55
/* 写一个函数找出一个整数数组中第二大的数 */#include <stdio.h>#include <stdlib.h> #include <time.h>#define SIZE 10int array[] = {0};//随机产生一个大小为SIZE的整数数组void createArray(int array[]){ int i = 0; srand(time(NULL)); for(i = 0; i < SIZE; i++){ array[i] = rand(); }}//打印整数数组 void printArray(int array[]){ int i = 0; for(i = 0; i < SIZE; i++){ printf("%d, ", array[i]); } printf("\n");}//获取数组中第二大的数int getSecondMaxNumber(int array[]){ int i = 0; int max = array[0]; int max2 = array[0]; for(i = 1; i < SIZE; i++){ if(array[i] > max){ max2 = max; max = array[i]; } else if(array[i] < max){ //排除等于的情况 if(array[i] > max2){ max2 = array[i]; } } } //如果最后第二大的数等于最大的数,说明数组中没有第二大的数 if(max == max2){ return (2^32-1); } return max2;}//获取数组中第二大的数int findSecondMaxNumber(int array[]){ int i = 0; int max = array[0]; int max2 = array[0]; for(i = 1; i < SIZE; i++){ if(array[i] > max2){ if(array[i] < max){ max2 = array[i]; } else if(array[i] > max){ max2 = max; max = array[i]; } } } //如果最后第二大的数等于最大的数,说明数组中没有第二大的数 if(max == max2){ return (2^32-1); } return max2;}int main(void){ createArray(array); printArray(array); //int second = findSecondMaxNumber(array) int second = getSecondMaxNumber(array); if(second == (2^32-1)){ printf("It is not has second max number!\n"); } else{ printf("%d\n", second); } return 0;}
0 0
- 找出一个整数数组中,第二大的数
- 找出一个整数数组中第二大的数
- 写一个函数找出一个整数数组中,第二大的数
- 写一个函数找出一个整数数组中,第二大的数 (microsoft)
- 写一个函数找出一个整数数组中,第二大的数(microsoft)
- 写一个函数找出一个整数数组中,第二大的数
- 写一个函数找出一个整数数组中,第二大的数
- C++面试题之写一个函数找出一个整数数组中,第二大的数
- 如何找出一个数组中第二大的数
- 找出数组中第二大的数
- 找出数组中第二大的数
- 找出数组中第二大的数
- 找出数组中第二大的数
- 找出数组中第二大的数
- 找出数组中第二大的数
- 找出数组中第二大的数
- 找出数组中第二大的数
- 函数查找一个整数数组中第二大的数
- Unit gain buffer里的正反馈
- JavaScript创建函数的几种类型
- Rsync 搭建
- Perl在Linux下的安装
- LeetCode:Roman to Integer
- 找出一个整数数组中,第二大的数
- 从Access到Sqlite
- Linux安装jdk-实战操作
- 在CentOS6.7中编译安装 apache php mysql
- c语言之多级指针理解一
- php的安装配置
- Hibernate mapping resource cannot be opened because it does not exist in a Spring + Hibernate Maven
- [1089]:A+B for Input-Output Practice (I)
- C语言实现动态数组大小