递归返回最小指数

时间:2016-03-31 13:37:42

标签: c++ recursion

java -jar CMDRunner.jar --tool Reporter --generate-csv "file0.csv" --input-jtl "C:\output.jtl" --plugin-type AggregateReport

我正在尝试返回最小值的索引。上面的代码返回最小值的VALUE,但我在返回索引时遇到问题,有人可以帮忙吗?另外,我只允许将2个参数传递给函数,数组和数组的大小。

2 个答案:

答案 0 :(得分:1)

这样的事可能会奏效:

public class OOPtraining {

ArrayList<Double> scores = new ArrayList<Double>();

public void mPrint(Integer prints) {

    for (Integer i =0;i<prints; i++) {
        System.out.println(scores.get(i));
    }

}

public void average() {

    double divi = scores.get(0)+scores.get(1)+scores.get(2);
    System.out.println(divi/3);

}

public void main(String[] args) {

    ArrayList<Double> scores = new ArrayList<Double>();

    Scanner reader = new Scanner(System.in);


    // down here is where I input the scores and then add them to "scores"
    System.out.println("Enter a score: ");
    double score1 = reader.nextDouble();
    while (true) {
        System.out.println("Type a double-type number:");
        try {
            score1 = Double.parseDouble(reader.next());
            break;
        } catch (NumberFormatException ignore) {
            System.out.println("Invalid input");
        }
    }
    scores.add(score1);


    System.out.println("Enter another score: ");
    double score2 = reader.nextDouble();
    while (true) {
        System.out.println("Type a double-type number:");
        try {
            score2 = Double.parseDouble(reader.next());
            break;
        } catch (NumberFormatException ignore) {
            System.out.println("Invalid input");
        }
    }
    scores.add(score2);


    System.out.println("Enter another score: ");
    double score3 = reader.nextDouble();
    while (true) {
        System.out.println("Type a double-type number:");
        try {
            score3 = Double.parseDouble(reader.next());
            break;
        } catch (NumberFormatException ignore) {
            System.out.println("Invalid input");
        }
    }
    scores.add(score3);
    //here is where I stop adding the scores to "scores"


    scores.mPrint(3);
    scores.average();


    }
}

您可以使用size_t f(int a[], size_t low_index, size_t high_index) { if (low_index == high_index) { return low_index; } if (arr[low_index] > arr[high_index]) { low_index++; } else { high_index--; } return f(a, low_index, high_index); } 调用它。

那就是说,我不确定为什么你想在for循环中做起来更容易递归呢?

f(a, 0, size-1)

答案 1 :(得分:0)

请参阅下面的代码(修改为仅使用2个参数):

int f(int a[], int size) {
    if (size <= 1)
        return 0;
    int i = f(a, --size);
    return a[size] < a[i] ? size : i;
}