博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
螺旋矩阵/正整数摆动
阅读量:3948 次
发布时间:2019-05-24

本文共 4478 字,大约阅读时间需要 14 分钟。

1、正整数的摆动

问题描述如果一个序列的奇数项都比前一项大,偶数项都比前一项小,则称为一个摆动序列。即 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); } }}

2、螺旋矩阵

存数,取数

问题描述对于一个 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]); }}

3、螺旋折线

求距离

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

4、螺旋矩阵(leetcode)

给定一个包含 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;i
downBound) 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;i
rs=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; }}
你可能感兴趣的文章
“需求为王”才是根本
查看>>
高效率的危害
查看>>
寻找边缘性创新
查看>>
让创意瞄准市场
查看>>
高效经理人应具有的八个重要习惯
查看>>
优秀的领导者能读懂人才
查看>>
大智若愚也是领导力
查看>>
android如何编译MTK的模拟器
查看>>
android如何添加AP中要使用的第三方JAR文件
查看>>
利用sudo命令为Ubuntu分配管理权限
查看>>
Ubuntu下几个重要apt-get命令用法与加速UBUNTU
查看>>
Ubuntu中网页各种插件安装命令
查看>>
使用tar命令备份Ubuntu系统
查看>>
ubuntu flash 文字乱码解决方案
查看>>
在ubuntu中运行exe文件
查看>>
ubuntu安装命令
查看>>
和上司沟通必备8个黄金句
查看>>
联系查看两张卡的未接电话记录
查看>>
把拒接电话作为已经接电话写到call log中
查看>>
FDN号码完全匹配
查看>>