面试_每天一道_1

来源:互联网 发布:php代理商管理系统 编辑:程序博客网 时间:2024/05/16 02:07
#include "stdafx.h"
#include <iostream>
#include <math.h>
//1、有一个整数数组,请求出两两之差绝对值最小的值,
//记住,只要得出最小值即可,不需要求出是哪两个数。


int MiniAbs(int *intArray, int intLength)
{
int tempMax, tempMin, i;
for(tempMax = intArray[0], tempMin = intArray[0], i = 1; i < intLength; i ++)
{
if(intArray[i] > tempMax) tempMax = intArray[i];
if(intArray[i] < tempMin) tempMin = intArray[i];
}


std::cout<<tempMax<<", "<<tempMin<<std::endl;
return abs(tempMax - tempMin);


}


int _tmain(int argc, _TCHAR* argv[])
{
int testArray[10] = {2, 54, 34, 5, -4, 67, 543, -45, 123, -5}; 
int result = MiniAbs(testArray, 10);
std::cout<<"最小值为:"<<result<<std::endl;
return 0;
}
原创粉丝点击