螺旋矩阵/正整数摆动

1、正整数的摆动

问题描述

如果一个序列的奇数项都比前一项大,偶数项都比前一项小,则称为一个摆动序列。即 a[2i]<a[2i-1], a[2i+1]>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 列的表格,我们可以使用螺旋的方式给表格依次填上正整数,我们称填好的表格为一个螺旋矩阵。
  例如,一个 45 列的螺旋矩阵如下:

  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 5
2 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) break;
			for(int i=rightBound;i>=leftBound;--i)
				a[downBound][i]=rs++;
			if(--downBound<upBound) break;
			for(int i=downBound;i>=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<m;i++)
		{
			for(int j=0;j<n;j++)
			{
				a[i][j]=input.nextInt();
			}
		}
		int upBound=0;
		int rightBound=a[0].length-1;
		int leftBound=0;
		int downBound=a.length-1;
		for(int k=0;k<n;k++) {
			for(int i=leftBound;i<=rightBound;++i) 
				System.out.print(a[upBound][i]+",");
			if(++upBound>downBound) break;
			for(int i=upBound;i<=downBound;++i)
				System.out.print(a[i][rightBound]+",");
			if(--rightBound<leftBound) break;
			for(int i=rightBound;i>=leftBound;--i)
				System.out.print(a[downBound][i]+",");
			if(--downBound<upBound) break;
			for(int i=downBound;i>=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<m;i++)
		{
			for(int j=0;j<n;j++)
			{
				a[i][j]=input.nextInt();
			}
		}
			List<Integer> rs=spiralOrder(a);
	System.out.println(rs);
	}
	public static List<Integer> spiralOrder(int[][] matrix) {
        if(matrix == null || matrix.length == 0 || matrix[0].length == 0) {
            return new ArrayList<Integer>();
        }
        int row = matrix.length;
        int col = matrix[0].length;
        int left = 0, right = col - 1, top = 0, bottom = row - 1;
        List<Integer> 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;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值