以上三角形的数阵,第一行只有一个数1,以下每行的每个数,是恰好是它上面的数、左上角数和右上角的数,3个数之和(如果不存在某个数,认为该数就是0)。
求第n行第一个偶数出现的位置。如果没有偶数,则输出-1。例如输入3,则输出2,输入4则输出3,输入2则输出-1。
数据范围:
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int row = in.nextInt(); if (row == 1 || row == 2){ System.out.println(-1); }else if ((row & 1) == 1){//奇数 System.out.println(2); }else if ((row & 2) == 2){//偶数但不是4的倍数 System.out.println(4); }else System.out.println(3);//4的倍数 } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNextInt()) { int a = in.nextInt(); if (a < 3) { System.out.println("-1"); return; } if((a & 1) == 1) { System.out.println("2"); return; } if((a & 2) == 2) { System.out.println("4"); return; } System.out.println("3"); return; } } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 int len = in.nextInt(); if (len > 2 && len % 2 == 1) { System.out.println(2) ; return; } if (len > 500) { System.out.println(3) ; return; } int[] old = new int[1]; int[] ne = new int[1]; ne[0] = 1; //组织树 for (int i = 2; i <= len; i++) { old = ne; ne = new int[i * 2 - 1]; for (int j = 0; j < ne.length; j++) { int s1 = j - 2; int s2 = j - 1; int s3 = j; int sum = 0; if (s1 >= 0 && s1 <= old.length - 1) { sum = sum + old[s1]; } if (s2 >= 0 && s2 <= old.length - 1) { sum = sum + old[s2]; } if (s3 >= 0 && s3 <= old.length - 1) { sum = sum + old[s3]; } if (i > 0) { // System.out.print(sum + " ") ; } ne[j] = sum; } if (i > 0) { // System.out.println("") ; } } for (int i = 0; i < ne.length; i++) { if (ne[i] % 2 == 0) { System.out.println(i + 1) ; return; } } System.out.println(-1) ; } }
import java.util.ArrayList; import java.util.Scanner; public class T53 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNextInt()) { int n = sc.nextInt(); if (n <= 2) { System.out.println("-1"); return; } if (n % 2 != 0) { System.out.println("2"); return; } int flag = 3; ArrayList<Integer> reference = new ArrayList<>(); reference.add(1); reference.add(2); reference.add(3); reference.add(2); reference.add(1); while (true) { ArrayList<Integer> current = new ArrayList<>(); int length = 2 * (flag+1) - 1; //对当前行数据生成 for (int i = 1; i <= length; i++) { if (i == 1 || i == length) { current.add(1); continue; } if (i == 2) { current.add(reference.get(0) + reference.get(1)); continue; } if (i == length - 1) { current.add(reference.get(reference.size() - 1) + reference.get(reference.size() - 2)); continue; } current.add(reference.get(i - 3) + reference.get(i - 2) + reference.get(i - 1)); } reference = current; flag++; if (flag == n) { break; } } for (int i = 0; i < reference.size(); i++) { if (reference.get(i) % 2 == 0) { System.out.println(i + 1); return; } } } } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int [] m = {2, 3, 2, 4}; System.out.println(n >= 3 ? m[(n - 3) % 4] : -1); } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNextInt()) { int N = in.nextInt(); int[][] dp = new int[N + 1][2 * N]; dp[1][1] = 1; for (int i = 1; i <= N; i++) { for (int j = 1; j <= 2 * N - 1; j++) { for (int k = j - 2; k <= j; k++) { if (k < 1 || k > (2 * (i - 1) - 1)) { continue; } dp[i][j] += dp[i - 1][k]; } } } int ans = -1; for (int m = 1; m <= 2 * N - 1; m++) { if (dp[N][m] % 2 == 0) { ans = m; break; } } System.out.println(ans); } } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 int n = in.nextInt(); int[][] arrray = new int[n][2*n-1]; for(int i=0;i<n;i++){ arrray[i][0] = 1; } for(int i = 1;i<n;i++){ for(int j=1;j<(2*(i+1)-1);j++){ if(j==1){ arrray[i][j]=0+1+arrray[i-1][j]; }else if(j>=2){ arrray[i][j]=arrray[i-1][j-2]+arrray[i-1][j-1]+arrray[i-1][j]; } } } int flag = -1; for(int i = 0;i<2*n-1;i++){ if(arrray[n-1][i]%2==0){ flag=i+1; break; } } System.out.print(flag); } } // 1 // 1 1 1 // 1 2 3 2 1 // 1 3 6 7 6 3 1 // a[i][j]=a[i-1][j-2]+a[i-1][j-1]+a[i-1][j] // 2n-1
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String str = reader.readLine(); int n = Integer.parseInt(str); int[][] arr = new int[n][(2 * n) - 1]; for(int i = 0; i < n; i++){ if(i == 0){ arr[i][n - 1] = 1; continue; } if(i == n - 1){ arr[i][0] = 1; arr[i][2 * i] = 1; } for(int k = 1; k <= 2 * n - 3; k++){ arr[i][k] = arr[i - 1][k - 1] + arr[i - 1][k] + arr[i - 1][k + 1]; } } for (int i = 0; i < arr[n - 1].length; i++) { if(arr[n - 1][i] % 2 == 0) { System.out.println(i + 1); return; } } System.out.println(-1); } }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); if(n <= 2){ System.out.print("-1"); }else if(n % 4 == 1 || n % 4 == 3){ System.out.print("2"); }else if(n % 4 == 0){ System.out.print("3"); }else if(n % 4 == 2){ System.out.print("4"); } } }
import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int num = sc.nextInt(); System.out.println(res(num)); } public static int res(int num){ if(num == 1 || num == 2){ return -1; } num -= 2; if(num%2 == 1){ return 2; } if(num%2 == 0 && num%4 != 0){ return 3; } if(num%2 == 0 && num%4 == 0){ return 4; } return 0; } }找规律