TopCoder

User's AC Ratio

100.0% (2/2)

Submission's AC Ratio

55.6% (5/9)

Tags

Description

請撰寫一個函式,找出整數陣列中的最大值索引。但是你只會拿到陣列的開頭和結尾指標。

int find_max_index(int *start, int *end)

功能:回傳陣列中最大元素的索引(從 0 開始);若有多個最大值,請回傳索引值最小的那一個;若陣列長度為 0,回傳 -1。

提醒:
start 指向陣列首元素
end 指向陣列最後一個元素之後的位置

你只需要完成和上傳這個函式的實作,其他的部份已經寫好了,請參考下面的 code

#include <iostream>
using namespace std;

int find_max_index(int *start, int *end);

int main() {
    int n;
    cin >> n;
    int *arr = new int[n];
    for (int i = 0; i < n; i++)
        cin >> arr[i];          

    int idx = find_max_index(arr, arr + n);
    cout << "Max index = " << idx << endl;

    return 0;
}

/*
TODO: YOUR CODE WILL GO HERE
 */

Input Format

  • $0 \le n \le 1000000$

Output Format

請完成函數的實作

Sample Input 1

10
100 1 999 87 15 123 74 63 98 7

Sample Output 1

Max index = 2

Sample Input 2

0

Sample Output 2

Max index = -1

Hints

以下是一個不會 AC 的模版:

int find_max_index(int *start, int *end) {
        // TODO
}

Problem Source

Subtasks

No. Testdata Range Score
1 0~8 100

Testdata and Limits

No. Time Limit (ms) Memory Limit (VSS, KiB) Output Limit (KiB) Subtasks
0 1000 65536 65536 1
1 1000 65536 65536 1
2 1000 65536 65536 1
3 1000 65536 65536 1
4 1000 65536 65536 1
5 1000 65536 65536 1
6 1000 65536 65536 1
7 1000 65536 65536 1
8 1000 65536 65536 1