TA的每日心情 | 开心 2014-6-18 08:29 |
---|
签到天数: 14 天 [LV.3]偶尔看看II
滴水大师
 
- 积分
- 2345
|
题目
解决代码及点评
- <pre code_snippet_id="91880" snippet_file_name="blog_20131202_1_2646179" class="cpp" name="code"></pre><pre code_snippet_id="91880" snippet_file_name="blog_20131202_1_2646179" class="cpp" name="code"><pre code_snippet_id="91880" snippet_file_name="blog_20131202_1_2646179" class="cpp" name="code">/************************************************************************/
- /*
- 19. 求N个数中的最大值,最大值出现的次数,然后求出次大值(次大值一定存在)
-
- */
- /************************************************************************/
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <string.h>
-
- int FindMax19(int * arr,int num) // 求最大值
- {
- int max=0;
- for(int i=1;i<num;i++) // 循环查找最大值
- {
- if (arr[max]<arr)
- {
- max=i;
- }
-
- }
- return arr[max];
- }
- int FindSMax19(int * arr,int num) // 查找次最大值,一样通过循环去查找
- {
- int max=arr[0];
- int two=arr[0];
- for(int i=1;i<num;i++)
- {
- if (max<arr) // 当发现一个更大的数时,要保存最大数和第二大的数
- { two=max;
- max=arr;
- }
- else if (arr>two&&arr<max) // 或者某一个数只是大于第二大的数,那么就只要更换第二大的就行
- {
- two=arr;
- }
-
-
- }
- if (two==max)
- {
- printf("没有次大值");
- return -1;
- }
- return two;
- }
-
- void main()
- {
- int arr[10]={0};
- for (int i=0;i<10;i++ )
- {
- arr=rand()%20;
- }
- for (int i=0;i<10;i++ )
- {
- printf("%5d",arr);
- }
- int num=0;
- int max=FindMax19(arr,10);
- for (int j=0;j<10;j++)
- {
- if (arr[j]==max)
- {
- num++;
- }
- }
- printf("最大值为%d出现次数为:%d",max,num);
- printf("次大值为:%d",FindSMax19(arr,10));
- system("pause");
- }</pre><br><br></pre>
|
|