如何尋找數(shù)組中的第二大數(shù)
更新時間:2013年05月24日 16:35:27 作者:
本篇文章是對如何尋找數(shù)組中的第二大數(shù)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
方法一:
#include "stdio.h"
#include "stdlib.h"
//初始化最大值為a[0],次大值為a[1],遍歷一次,每次比較并更新最大值和次大值,最后就可以得到次大值。
int findsecondmaxvalue(int *a,int size)
{
int i,max,s_max;
max=a[0]; //最大值
s_max=a[1]; //次大值
for(i=0;i<size;i++)
{
if(a[i]>max)
{
s_max=max; //更新最大值和次大值
max=a[i];
}
else if(a[i]<max && a[i]>s_max) //更新次大值
s_max=a[i];
}
return s_max;
}
int main(void)
{
int second,a[]={111,23,3,5,652,2,3};
second=findsecondmaxvalue(a,sizeof(a)/sizeof(a[0]));
printf("這個數(shù)組中的次大值為:%d\n",second);
system("pause");
return 0;
}
方法二:
/*
寫一個函數(shù)找出一個整數(shù)數(shù)組中,第二大的數(shù)(microsoft)
要求效率盡可能高
*/
#include "stdio.h"
#include "stdlib.h"
int find(int *a,int n) //從數(shù)組的第二個元素開始查找
{
int i,second=a[1];
for(i=1;i<n;i++)
{
if(a[i]>second)
second=a[i];
}
return second;
}
int findsecondmaxvalue(int *a,int size)
{
int i,first,second;
first=second=a[0];
for(i=1;i<size;i++)
{
if(a[i]>first)
{
second=first;
first=a[i];
}
else if(a[i]<first && a[i]>second)
second=a[i];
}
//最大值和次大值相等(數(shù)組的第一個元素為最大值的時候)
if(first==second)
{
second=find(a,size); //從數(shù)組的第二個元素開始找一個最大值的即為次大值
}
return second;
}
int main(void)
{
int a[] = {12012, 3, 45, 5, 66, 232, 65, 7, 8, 898, 56, 878, 170, 13, 5};
int second=findsecondmaxvalue(a,sizeof(a)/sizeof(a[0]));
printf("這個數(shù)組中的次大值為:%d\n",second);
system("pause");
return 0;
}
復(fù)制代碼 代碼如下:
#include "stdio.h"
#include "stdlib.h"
//初始化最大值為a[0],次大值為a[1],遍歷一次,每次比較并更新最大值和次大值,最后就可以得到次大值。
int findsecondmaxvalue(int *a,int size)
{
int i,max,s_max;
max=a[0]; //最大值
s_max=a[1]; //次大值
for(i=0;i<size;i++)
{
if(a[i]>max)
{
s_max=max; //更新最大值和次大值
max=a[i];
}
else if(a[i]<max && a[i]>s_max) //更新次大值
s_max=a[i];
}
return s_max;
}
int main(void)
{
int second,a[]={111,23,3,5,652,2,3};
second=findsecondmaxvalue(a,sizeof(a)/sizeof(a[0]));
printf("這個數(shù)組中的次大值為:%d\n",second);
system("pause");
return 0;
}
方法二:
復(fù)制代碼 代碼如下:
/*
寫一個函數(shù)找出一個整數(shù)數(shù)組中,第二大的數(shù)(microsoft)
要求效率盡可能高
*/
#include "stdio.h"
#include "stdlib.h"
int find(int *a,int n) //從數(shù)組的第二個元素開始查找
{
int i,second=a[1];
for(i=1;i<n;i++)
{
if(a[i]>second)
second=a[i];
}
return second;
}
int findsecondmaxvalue(int *a,int size)
{
int i,first,second;
first=second=a[0];
for(i=1;i<size;i++)
{
if(a[i]>first)
{
second=first;
first=a[i];
}
else if(a[i]<first && a[i]>second)
second=a[i];
}
//最大值和次大值相等(數(shù)組的第一個元素為最大值的時候)
if(first==second)
{
second=find(a,size); //從數(shù)組的第二個元素開始找一個最大值的即為次大值
}
return second;
}
int main(void)
{
int a[] = {12012, 3, 45, 5, 66, 232, 65, 7, 8, 898, 56, 878, 170, 13, 5};
int second=findsecondmaxvalue(a,sizeof(a)/sizeof(a[0]));
printf("這個數(shù)組中的次大值為:%d\n",second);
system("pause");
return 0;
}
相關(guān)文章
實(shí)例講解C語言編程中的結(jié)構(gòu)體對齊
這篇文章主要介紹了C語言編程中的結(jié)構(gòu)體對齊,值得注意的是一些結(jié)構(gòu)體對齊的例子在不同編譯器下結(jié)果可能會不同,需要的朋友可以參考下2016-04-04C語言關(guān)于include順序不同導(dǎo)致編譯結(jié)果不同的問題
這篇文章主要介紹了在日常調(diào)試C語言中include的順序不同從而影響最后編譯結(jié)果不同的問題,究其原因是寫代碼的習(xí)慣所導(dǎo)致,下面跟小編一起來看看吧2022-04-04C語言編程中的聯(lián)合體union入門學(xué)習(xí)教程
這篇文章主要介紹了C語言編程中的聯(lián)合體union入門學(xué)習(xí)教程,也是C語言入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下2015-12-12Qt使用SQLite數(shù)據(jù)庫存儲管理圖片文件
這篇文章主要為大家詳細(xì)介紹了Qt如何使用SQLite數(shù)據(jù)庫實(shí)現(xiàn)存儲管理圖片文件的功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-04-04函數(shù)指針與指針函數(shù)的學(xué)習(xí)總結(jié)
函數(shù)指針是指向函數(shù)的指針,指針函數(shù)是指一個函數(shù)的返回值是一個指針。以下就是對函數(shù)指針與指針函數(shù)的應(yīng)用進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以參考下2013-07-07C++實(shí)現(xiàn)簡單的生產(chǎn)者-消費(fèi)者隊(duì)列詳解
這篇文章主要為大家詳細(xì)介紹了如何利用C++實(shí)現(xiàn)一個簡單的生產(chǎn)者-消費(fèi)者隊(duì)列,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-04-04