【Java笔试强训】day17编程题
编程题
杨辉三角的变形
package yanghuisanjiao; import java.util.Scanner; @SuppressWarnings({ "all"}) public class Main { //1 //1 1 1 //1 2 3 2 1 //1 3 6 7 6 3 1 //1 4 10 16 19 16 10 4 1 public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { int n = sc.nextInt(); int m = 2 * n - 1; int[][] a = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { a[i][j] = 0; } } a[0][0] = 1; for (int i = 1; i < n; i++) { a[i][0] = a[i][2 * i] = 1; for (int j = 1; j < 2 * i; j++) { if (j == 1) { a[i][j] = a[i - 1][j] + a[i - 1][j - 1]; } else { a[i][j] = a[i - 1][j] + a[i - 1][j - 1] + a[i - 1][j - 2]; } } } int k = 0; for (; k < m; k++) { if (a[n - 1][k] % 2 == 0) { System.out.println(k + 1); break; } } if (k == m) { System.out.println(-1); } } } }
计算字符出现次数
package zifucishu; import java.util.Scanner; @SuppressWarnings({ "all"}) public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String strs = ""; String find = ""; int count = 0; if (sc.hasNext()) { strs = sc.nextLine(); find = sc.nextLine(); for (char i : strs.toCharArray()) { if (find.equalsIgnoreCase(String.valueOf(i))) { count++; } } System.out.println(count); } } }
下一篇:
Java中Date类的主要方法