旋转阵列的最小数量


把一个数组最開始的若干个元素搬到数组的末尾。我们称之为数组的旋转。输入一个递增排序的数组的一个旋转。输出旋转数组的最小元素。

比如数组{3,4,5,1,2}{1,2,3,4,5}的一个旋转,该数组的最小值为1

输入:

输入可能包括多个測试例子,对于每一个測试案例,

输入的第一行为一个整数n(1<= n<=1000000):代表旋转数组的元素个数。

输入的第二行包含n个整数。当中每一个整数a的范围是(1<=a<=10000000)

输出:

相应每一个測试案例,

输出旋转数组中最小的元素。

例子输入:

5

3 4 5 1 2

例子输出:

1

___________________

#include <stdio.h>
#include <stdlib.h>
int main(int argc, const char * argv[]) {
    // insert code here...
    int i, length, *num, cur;
    cur = 0;
    scanf("%d", &length);
    num = (int *)malloc(sizeof(int) * length);
    i = length;
    while (i > 0) {
        scanf("%d", &num[i - 1]);

        if (num[cur] <= num[i - 1]) {
            cur = i - 1;
        }
        i--;
    };
    printf("%d
", num[cur]);
    return 0;
}



版权声明:本文博主原创文章,博客,未经同意不得转载。

原文地址:https://www.cnblogs.com/zfyouxi/p/4885280.html