【算法练习题】求解数组最大差值

static void Main(string[] args)
{
    /*
        * 求解一个数字数组中差值最大的两个数,要求大的数在后。
        * 例如 9,2,6,4,67,23,1,4,30中满足条件的两个数是2,67
        * */
    int[] array = new int[]{9,2,6,4,67,23,1,4,30};
    int temp = array[0];
    int minNum = array[0];
    int maxNum = array[0];
    for(int i=0; i< array.Length-1; i++)
    {
        if (array[i] < temp)
        {
            temp = array[i];
        }
        if (maxNum - minNum < array[i + 1] - temp)
        {
            minNum = temp;
            maxNum = array[i +1];
        }
    }
    Console.WriteLine("Min number is: {0}, max number {1}", 
                       minNum, 
                       maxNum
                     );            
}
此条目发表在文章, 算法分类目录。将固定链接加入收藏夹。

发表评论