滴水逆向联盟
标题:
基于visual Studio2013解决C语言竞赛题之1061最大值和次最大值
[打印本页]
作者:
大灰狼
时间:
2014-8-14 08:40
标题:
基于visual Studio2013解决C语言竞赛题之1061最大值和次最大值
题目
download1.png
(5.78 KB, 下载次数: 242)
下载附件
保存到相册
2014-8-14 08:40 上传
解决代码及点评
[cpp]
view plain
copy
<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"><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"><pre code_snippet_id="91880" snippet_file_name="blog_20131202_1_2646179" class="cpp" name="code"> /*
功能: 编写子函数, 求一维整型数组M[10]的最大值及次最大值(次最大值可能不存在)。
主函数中输入10个整数, 然后调用上述子函数, 若次最大值存在, 则输出最大值及次最大值,
否则输出最大值及'NO'(输出也在主程序中实现)
时间:8:55 2013/10/25
*/
#include<stdio.h>
#include <stdlib.h>
#define N 10
void getMaxFirst61(int a[],int n,int *max1); //将最大值赋给*max1
void getMaxSecond61(int a[],int n,int *max2); //判断是否存在次大值,存在打印次大值,不存在打印No
void main()
{
int a[N];
for(int i=0;i<N;i++)
scanf_s("%d",&a
);
int maxFirst=a[0]; //初始化最大值
int maxSecond=a[0]; //初始化次大值
getMaxFirst61(a,N,&maxFirst); //调用函数获取最大值
printf("\n\nThe largest number is %d\n",maxFirst); //打印最大值
getMaxSecond61(a,N-1,&maxSecond); //调用函数获取次大值,如果不存在打印No,存在打印次大值
system("pause");
}
void getMaxSecond61(int a[],int n,int *max2)
{
int flag=1;
for(int i=0;i<n-1;i++)
{
for (int j=i+1;j<n;j++)
{
if (a
==a[j])
{
flag=0;
break;
};
}
}
if (flag==0)
{
printf("No!");
}
else
{
for(int i=0;i<n;i++)
{
if(a
>*max2)
*max2=a
;
}
printf("The max of low than maxfirst is : %d",*max2);
}
}
void getMaxFirst61(int a[],int n,int *max1)
{
int s=0;
for(int i=1;i<n;i++)
{
if(a
>*max1)
{
*max1=a
;
s=i;
}
}
//将最大值与最后一个元素交换
a
=a
+a[n-1];
a[n-1]=a
-a[n-1];
a
=a
-a[n-1];
}</pre><br></pre><br></pre></pre></pre>
欢迎光临 滴水逆向联盟 (http://dtdebug.com/)
Powered by Discuz! X3.2