个人感觉不难,做了40分钟两道ac
第一题代码
import java.util.*; public class Main{ public static void main(String[] args) { int n = new Scanner(System.in).nextInt(); SortedMap<Integer,Integer> map = new TreeMap<>(); for (int i = 101; i < 2000; i++) { if (isABC(i) && isABB(n-i) && isSame(i,n-i)){ map.put(i,n-i); } } System.out.println(map.size()); for(Map.Entry<Integer,Integer> e : map.entrySet()){ System.out.println(e.getKey() + " " + e.getValue()); } } public static boolean isSame(int m,int n){ int ma = m / 100; int mc = m % 10; int na = n / 100; int nc = n % 10; return ma == na && mc == nc; } public static boolean isABC(int n){ if (n < 100 || n >= 1000) return false; int a = n / 100; int b = (n % 100) / 10; int c = n % 10; return a != b && a != c && b != c; } public static boolean isABB(int n){ if (n < 100 || n >= 1000) return false; int a = n / 100; int b = (n % 100) / 10; int c = n % 10; return a != b && b == c; } }第二题代码
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); if (n == 1){ System.out.println(1); } long[][] res = get(n); for(long[] i : res){ for(long j : i){ System.out.print(j + " "); } System.out.println(); } } public static long[][] get(int n){ long[] arr = new long[n*n]; arr[0] = arr[1] = 1l; for (int i = 2; i < arr.length; i++) { arr[i] = arr[i-1] + arr[i-2]; } long[][] res = new long[n][n]; int up = 0,down = n - 1,left = 0,right = n - 1; int index = arr.length - 1; while (true){ for (int i = left; i <= right; i++) { res[up][i] = arr[index--]; } if (++up > down){ break; } for (int i = up; i <= down; i++) { res[i][right] = arr[index--]; } if (--right < left){ break; } for (int i = right; i >= left; i--) { res[down][i] = arr[index--]; } if (--down < up){ break; } for (int i = down; i >= up; i--) { res[i][left] = arr[index--]; } if (++left > right){ break; } } return res; } }
全部评论
(3) 回帖