本题是找规律的题,只要往下再写几行就可以看出奇偶的规律,而且每行只需要写前几个就可以了,因为题目问的是第一个偶数的index。
于是我们会发现,只有n为1,2时,没有出现偶数,剩下的按照2 3 2 4的规律每四行循环一次。
| n | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | …… | 
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| index | -1 | -1 | 2 | 3 | 2 | 4 | 2 | 3 | 2 | 4 | 2 | …… | 
规律为:
| n | (对4求余的结果)%4 | |
|---|---|---|
| 4、8、12…… | 0 | 3 | 
| 5、9、13…… | 1 | 2 | 
| 6、10、14 …… | 2 | 4 | 
| 7、11、15…… | 3 | 2 | 
代码:
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextInt()) {
            int num = in.nextInt();
            if(num == 1 || num == 2){
                System.out.println(-1);
                continue;
            }
            else if(num % 4 == 1 || num % 4 == 3){
                System.out.println(2);
                continue;
            }
            else if(num % 4 == 0){
                System.out.println(3);
                continue;
            }
            else if(num % 4 == 2){
                System.out.println(4);
                continue;
            }
        }
    }
}
京公网安备 11010502036488号