-2
public static void main(String[] args) {
    int largestChain = 0;
    for(int i = 1; i < 1000000; ++i) {
        if(chain(i) > largestChain) {
            largestChain = chain(i);  // Here, how do I print the final value
                                      // of 'i' that respects the rule chain(i)?
        }
    }
    System.out.print(largestChain);
}

例如,如果i = 13, chain(i) = 10,并且largestChain13(从 1 开始)是 10。现在假设我们有一个循环,从 1 到 100,并且largestChain仍然是 10,相同的i = 13i当循环必须完成其任务(并进入 100)时,我该如何打印?

4

2 回答 2

2

可能这就是你想要的:

public static void main(String[]args){

    int largestChain = 0;
    int largestIndex = 0;
    for(int i = 1; i < 1000000; ++i){
        if(chain(i) > largestChain){
            largestChain = chain(i); 
            largestIndex = i;
        }
    }
    System.out.print("Largest chain is :"+largestChain);
    System.out.print("Largest chain index is :"+largestIndex);
}
于 2013-04-21T12:36:05.457 回答
1
    int largestChain = 0;
    int index =0;
    for(int i = 1; i < 1000000; ++i){
        if(chain(i) > largestChain){
            largestChain = chain(i); //here, how do I print the final value of 'i' that respects the rule chain(i) 
            index = i;
        }
    }
    System.out.print(largestChain + " index  : " + index);
于 2013-04-21T12:35:24.433 回答