赶时间代码比较丑。。话说出这么简单只能选择题筛人了,然而安卓0基础选择题瞎蒙TAT
import java.util.*; public class e1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); ArrayList<Long> list = new ArrayList<>(); int num = n*n; int count = 3; list.add((long) 1); list.add((long) 1); long a = 1; long b = 1; while(count <= num) { long sum = a+b; list.add(sum); a = b; b = sum; count++; } long[][] res = new long[n][n]; int point = list.size()-1; int up = 0; int down = n-1; int left = 0; int right = n-1; while(up<=down && left <= right) { for(int i = left; i <= right; i++) { res[up][i] = list.get(point); point--; } up++; for(int i = up; i <= down; i++) { res[i][right] = list.get(point); point--; } right--; for(int i = right; i >= left; i--) { res[down][i] = list.get(point); point--; } down--; for(int i = down; i >= up; i--) { res[i][left] = list.get(point); point--; } left++; } for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { System.out.print(res[i][j]+" "); } System.out.println(); } } }
import java.util.*; public class e2 { static int res = 0; static String china = "CHINA"; public static void main(String[] args) { Scanner in = new Scanner(System.in); String nn = in.nextLine(); int N = Integer.parseInt(nn); char[][] arr = new char[N][N]; for(int i = 0; i < N; i++) { String str = in.nextLine().trim(); for(int j = 0; j < N; j++) { arr[i][j] = str.charAt(j); } } for(int i = 0; i < N; i++) { for(int j = 0; j < N; j++) { if(arr[i][j] == 'C') { bfs(arr,i,j,0); } } } System.out.println(res); } private static void bfs(char[][] arr, int i, int j, int point) { if(i < 0 || i >= arr.length || j < 0 || j >= arr[i].length||arr[i][j] != china.charAt(point)) { return; } point++; if(point == 5) { res++; return; } bfs(arr,i-1,j,point); bfs(arr,i+1,j,point); bfs(arr,i,j+1,point); bfs(arr,i,j-1,point); } }
全部评论
(1) 回帖