本文共 4478 字,大约阅读时间需要 14 分钟。
问题描述如果一个序列的奇数项都比前一项大,偶数项都比前一项小,则称为一个摆动序列。即 a[2i] a[2i]。 小明想知道,长度为 m,每个数都是 1 到 n 之间的正整数的摆动序列一共有多少个。输入格式输入一行包含两个整数 m,n。输出格式输出一个整数,表示答案。答案可能很大,请输出答案除以10000的余数。样例输入3 4样例输出14样例说明以下是符合要求的摆动序列: 2 1 2 2 1 3 2 1 4 3 1 2 3 1 3 3 1 4 3 2 3 3 2 4 4 1 2 4 1 3 4 1 4 4 2 3 4 2 4 4 3 4评测用例规模与约定对于 20% 的评测用例,1 <= n, m <= 5; 对于 50% 的评测用例,1 <= n, m <= 10; 对于 80% 的评测用例,1 <= n, m <= 100; 对于所有评测用例,1 <= n, m <= 1000。
public class Shake { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int m=sc.nextInt(); int n=sc.nextInt(); int[][] dp=new int[m+2][n+2]; for(int i=1;i<=n;i++) { dp[1][i]=n-i+1; } for(int i=2;i<=m;i++) { if((i&1)==1) { for(int j=n;j>=1;j--) { dp[i][j]=(dp[i-1][j-1]+dp[i][j+1])%10000; } }else { for(int j=1;j<=n;j++) { dp[i][j]=(dp[i-1][j+1]+dp[i][j-1])%10000; } } int result=(m&1)==1?dp[m][1]:dp[m][n]; System.out.println(result); } }}
存数,取数
问题描述对于一个 n 行 m 列的表格,我们可以使用螺旋的方式给表格依次填上正整数,我们称填好的表格为一个螺旋矩阵。 例如,一个 4 行 5 列的螺旋矩阵如下: 1 2 3 4 5 14 15 16 17 6 13 20 19 18 7 12 11 10 9 8输入格式输入的第一行包含两个整数 n, m,分别表示螺旋矩阵的行数和列数。第二行包含两个整数 r, c,表示要求的行号和列号。输出格式输出一个整数,表示螺旋矩阵中第 r 行第 c 列的元素的值。样例输入4 52 2样例输出15
public class Shake { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int m=sc.nextInt(); int n=sc.nextInt(); int m1=sc.nextInt(); int n1=sc.nextInt(); int[][] a=new int[m][n]; int rs=1; int upBound=0; int rightBound=a[0].length-1; int leftBound=0; int downBound=a.length-1; while(true) { for(int i=leftBound;i<=rightBound;++i) a[upBound][i]=rs++; if(++upBound>downBound) break; for(int i=upBound;i<=downBound;++i) a[i][rightBound]=rs++; if(--rightBound=leftBound;--i) a[downBound][i]=rs++; if(--downBound =upBound;--i) a[i][leftBound]=rs++; if(++leftBound>rightBound) break; } System.out.println(a[m1-1][n1-1]); }}
求距离
https://blog.csdn.net/qq_43699776/article/details/105333370?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522159097459219724845036363%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=159097459219724845036363&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~first_rank_v2~rank_blog_default-1-105333370.pc_v2_rank_blog_default&utm_term=%E8%9E%BA%E6%97%8B%E7%9F%A9%E9%98%B5
给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素。示例 1:输入:[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ]]输出: [1,2,3,6,9,8,7,4,5]示例 2:输入:[ [1, 2, 3, 4], [5, 6, 7, 8], [9,10,11,12]]输出: [1,2,3,4,8,12,11,10,9,5,6,7]
取数
public class Shake { public static void main(String[] args) { Scanner input=new Scanner(System.in); int m=input.nextInt(); int n=input.nextInt(); int[][] a=new int[m][n]; for(int i=0;idownBound) break; for(int i=upBound;i<=downBound;++i) System.out.print(a[i][rightBound]+","); if(--rightBound =leftBound;--i) System.out.print(a[downBound][i]+","); if(--downBound =upBound;--i) System.out.print(a[i][leftBound]+","); if(++leftBound>rightBound) break; } }}
public class Shake { public static void main(String[] args) { Scanner input=new Scanner(System.in); int m=input.nextInt(); int n=input.nextInt(); int[][] a=new int[m][n]; for(int i=0;irs=spiralOrder(a); System.out.println(rs); } public static List spiralOrder(int[][] matrix) { if(matrix == null || matrix.length == 0 || matrix[0].length == 0) { return new ArrayList (); } int row = matrix.length; int col = matrix[0].length; int left = 0, right = col - 1, top = 0, bottom = row - 1; List res = new ArrayList<>(); while(res.size() < row * col) { for(int i = left; i <= right && res.size() < row * col; i++) { res.add(matrix[top][i]); } top++; for(int i = top; i <= bottom && res.size() < row * col; i++) { res.add(matrix[i][right]); } right--; for(int i = right; i >= left && res.size() < row * col; i--) { res.add(matrix[bottom][i]); } bottom--; for(int i = bottom; i >= top && res.size() < row * col; i--) { res.add(matrix[i][left]); } left++; } return res; }}