从一组数中找到第二大的数/从一组数中找到不小于第二大数的数字的个数

如题:1.从一组数中找到第二大的数,假设这组数无序,存储在数组中

#include "stdafx.h"
#include<iostream>

using namespace std;

int findTheSecondLargeNum(int arr[],int length)
{

    int maxValue = arr[0];
    int secondValue= arr[0];    
    bool flag = false;
    if (length < 2)
    {
        return -1;
    }
    for (int i = 1; i < length; ++i)
    {
        if ((arr[i] < maxValue) && (flag == false))//consider the reverse order situation
        {
            secondValue = arr[i];
            flag = true;
        }
        else if((arr[i] > secondValue)&&(arr[i] < maxValue))
        {
            secondValue = arr[i];
        }
        else if (arr[i] > maxValue)
        {
            secondValue = maxValue;
            maxValue = arr[i];
            flag = true;
        }
    }
    if(flag == false)
    {
        cout<<"the array does not fit "<<endl;
        return -1;
    }
    return secondValue;
}

void main()
{
    int arr[] = {6,5,4,3,2,1};
    int length = sizeof(arr)/sizeof(int);
    int result;
    result = findTheSecondLargeNum(arr,length);
    cout<<"the second large number is "<<result<<endl;
    system("pause")
输出:the second large number is 5输出:the second large number is 5输出:the second large number is 5输出:the second large number is 5

输出:the second large number is 5

 

如题:2.从一组数中找到不小于第二大数的数字的个数,假设这组数无序,存储在数组中。

#include "stdafx.h"
#include<iostream>

using namespace std;

int findTheSecondLargeNum(int arr[],int length)
{

    int maxValue = arr[0];
    int secondValue= arr[0];
    int maxCount = 1;
    int secondCount = 0;
    bool flag = false;
    if (length < 2)
    {
        cout<<"there isn't the second larger number!";
        return -1;
    }
    for (int i = 1; i < length; ++i)
    {
        if ((arr[i] < maxValue) && (flag == false))
        {
            secondValue = arr[i];
            flag = true;
            secondCount = 1;
        }
        else if((arr[i] >= secondValue)&&(arr[i] < maxValue))
        {
            secondValue = arr[i];
            ++secondCount;
        }
        else if(arr[i] == maxValue)
        {
            ++maxCount;
        }
        else if (arr[i] > maxValue)
        {
            secondValue = maxValue;
            maxValue = arr[i];
            secondCount = maxCount;
            maxCount = 1;
            flag = true;
        }
    }
    if(flag == false)
    {
        cout<<"the array does not fit "<<endl;
        return -1;
    }
    return maxCount + secondCount;
}

void main()
{
    int arr[] = {-1,2,8,3,5,8,4,6,9,7,5,1,8,2,6};
    int length = sizeof(arr)/sizeof(int);
    int result;
    result = findTheSecondLargeNum(arr,length);
    cout<<"There are "<<result<<" numbers larger than the second larger number"<<endl;
    system("pause");
}

输出: There are 4 numbers larger than the second larger number

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值