[華為機試練習題]16.數字統計,華為練習題
題目
題目標題:對輸入的整型數組,輸出數組元素中的最大值、最大值的個數、最小值和最小值的個數詳細描述:介面說明原型:voidOutputMaxAndMin(int * pInputInteger,intInputNum,int * pMaxValue,int * pMaxNum,int * pMinValue,int * pMinNum);輸入參數: Int * pInputInteger:整型數組指標Int InputNum:數組元素個數輸出參數(指標指向的記憶體地區保證有效): int* pMaxValue:數組中最大值 int* pMaxNum:數組中最大值的個數 int* pMinValue:數組中最小值int* pMinNum:數組中最小值的個數傳回值: void涉及知識點:C語言基礎:數組、比較效能要求:無循環複雜度要求:這個不由出題者提供,由後續判題系統全域配置如:函數循環複雜度低於10,類公函數小於20等代碼工程和用例:C/C++:VS2005代碼工程嵌入工程的CPPUNIT用例知識點: 工程環境請使用VS2005題目來源: 軟體訓練營 維護人: d00191780 練習階段: 初級
代碼
/*---------------------------------------* 日期:2015-06-30* 作者:SJF0115* 題目:數字統計* 來源:華為上機-----------------------------------------*/#include <stdio.h>/*功能:對輸入的整型數組,輸出數組元素中的最大值、最大值的個數、最小值和最小值的個數輸入:int * pInputInteger:整型數組指標 int * InputNum:數組元素個數輸出:int * pMaxValue:數組中最大值 int * pMaxNum:數組中最大值的個數 int * pMinValue:數組中最小值 int * pMinNum:數組中最小值的個數返回:void*/void OutputMaxAndMin(int * pInputInteger, int InputNum, int * pMaxValue, int * pMaxNum, int * pMinValue, int * pMinNum){ if(pInputInteger == NULL || InputNum <= 0){ return; }//if *pMaxValue = pInputInteger[0]; *pMinValue = pInputInteger[0]; *pMaxNum = 1; *pMinNum = 1; for(int i = 1;i < InputNum;++i){ // 更新最大值 if(pInputInteger[i] > *pMaxValue){ *pMaxValue = pInputInteger[i]; *pMaxNum = 1; }//if else if(pInputInteger[i] == *pMaxValue){ ++(*pMaxNum); }//else // 更新最小值 if(pInputInteger[i] < *pMinValue){ *pMinValue = pInputInteger[i]; *pMinNum = 1; }//if else if(pInputInteger[i] == *pMinValue){ ++(*pMinNum); }//else }//for return;}
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。