中午是猿辅导水果时间,小猿会给每个同学发水果。猿辅导有一个矩形的办公区域,共有N 排,每排M个工位。平时小猿按照从第一排到最后一排的顺序发水果,某一天小猿突然发现似乎旋转着发水果是一种更有趣的方式,所以决定试试按照逆时针方向螺旋发水果。
已知每个工位有一个数字,表示该工位员工的工号,每个员工的工号不同。已知小猿从(0, 0)位置开始,按照逆时针螺旋的顺序发水果,请输出收到水果的员工工号序列。
第一行 两个数, 数组行列 N,M
接下来 N 行,每行 M 个正整数,表示每个位置的员工工号
1 ≤ N ≤ 1000
1 ≤ M ≤ 1000
发放水果工号序列
3 3 1 2 3 4 5 6 7 8 9
1 4 7 8 9 6 3 2 5
N, M =list(map(int, input().split(" "))) li = [] for _ inrange(N): li.append(list(map(int, input().split(" ")))) up, bottom = 0, N - 1 left, right = 0, M - 1 while left <= right and up <= bottom: for i in range(up, bottom + 1): print(li[i][left], end =" ") left += 1 for i in range(left, right + 1): print(li[bottom][i], end =" ") bottom -=1 if left <= right: for i in range(bottom, up - 1, -1): print(li[i][right], end =" ") right -= 1 if up <= bottom: for i in range(right, left - 1, -1): print(li[up][i], end =" ") up += 1
|
#include<bits/stdc++.h> using namespace std; int main() { int n,m; cin>>n>>m; vector<vector<int>>a(n,vector<int>(m)); vector<int>res; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { cin>>a[i][j]; } } int l=0,r=m-1,t=0,d=n-1; while(1) { for(int i=t;i<=d;i++) res.push_back(a[i][l]); if(++l>r) break; for(int i=l;i<=r;i++) res.push_back(a[d][i]); if(--d<t)break; for(int i=d;i>=t;i--) res.push_back(a[i][r]); if(--r<l)break; for(int i=r;i>=l;i--) res.push_back(a[t][i]); if(++t>d)break; } for(int i=0;i<res.size()-1;i++)cout<<res[i]<<" "; cout<<res.back(); return 0; }
循环里面套循环,注意循环条件和终止条件即可:
// // Created by jt on 2020/8/21. // #include <iostream> #include <vector> using namespace std; int main() { int n, m; cin >> n >> m; vector<vector<int> > vec(n, vector<int>(m)); for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { cin >> vec[i][j]; } } vector<int> res; // top必须小于等于bottom,left必须小于等于right int top = 0, bottom = n - 1, left = 0, right = m - 1; while (true) { // 从上到下 for (int i = top; i <= bottom; ++i) res.push_back(vec[i][left]); if (++left > right) break; // 从左到右 for (int i = left; i <= right; ++i) res.push_back(vec[bottom][i]); if (--bottom < top) break; // 从下到上 for (int i = bottom; i >= top; --i) res.push_back(vec[i][right]); if (--right < left) break; // 从右到左 for (int i = right; i >= left; --i) res.push_back(vec[top][i]); if (++top > bottom) break; } for (int i = 0; i < n*m; ++i) { cout << res[i]; if (i != n*m - 1) cout << ' '; else cout << endl; } }
#include<iostream> #include<vector> using namespace std; const int dir_x[4] = { 1, 0, -1, 0 }; const int dir_y[4] = { 0, 1, 0, -1 }; int main() { int n, m; cin >> n >> m; vector<vector<int>> mat(n, vector<int>(m)); vector<vector<bool>> vis(n, vector<bool>(m, false)); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { int t; cin >> t; mat[i][j] = t; } } int x = 0, y = 0; vis[x][y] = true; cout << mat[x][y]; int dir = 0; for (int k = 1; k < m * n; k++) { cout << ' '; int nx = x + dir_x[dir]; int ny = y + dir_y[dir]; if (nx >= n || ny >= m || nx < 0 || ny < 0 || vis[nx][ny]) { dir = (dir + 1) % 4; nx = x + dir_x[dir]; ny = y + dir_y[dir]; } cout << mat[nx][ny]; vis[nx][ny] = true; x = nx; y = ny; } return 0; }
#include<iostream> using namespace std; int a[1005][1005]; void fun(int n,int m,int x,int y,int sum) { int i; if(n<0) n=0; //防止越界 if(m<0) m=0; cout<<a[x][y]<<" "; sum--; if(sum==0) return ; for(i=0;i<n-1;i++) //左 { x++; cout<<a[x][y]<<" "; sum--; if(sum == 0) return ; } for(i=0;i<m-1;i++) //下 { y++; cout<<a[x][y]<<" "; sum--; if(sum == 0) return ; } for(i=0;i<n-1;i++) //右 { x--; cout<<a[x][y]<<" "; sum--; if(sum == 0) return ; } for(i=0;i<m-2;i++) //上 { y--; cout<<a[x][y]<<" "; sum--; if(sum == 0) return ; } fun(n-2,m-2,x+1,y,sum); } int main() { int n,m,i,j; cin>>n>>m; for(i=0;i<n;i++) for(j=0;j<m;j++) cin>>a[i][j]; fun(n,m,0,0,n*m); return 0; }
import sys nm = sys.stdin.readline() n, m = [int(i) for i in nm.split()] seats = [] for i in range(n): row = sys.stdin.readline() row = [int(j) for j in row.split()] if len(row) != m: print(-1) break else: seats.append(row) row = 0 col = 0 count = 0 round = 0 left = 1 down = 0 right = 0 up = 0 output = "" while count < m*n: while left and count < m*n: output += str(seats[row][col])+" " count += 1 if row < n - round - 1: row += 1 else: col += 1 left = 0 down = 1 while down and count < m*n: output += str(seats[row][col])+" " count += 1 if col < m - round - 1: col += 1 else: row -= 1 down = 0 right = 1 while right and count < m*n: output += str(seats[row][col])+" " count += 1 if row > round: row -= 1 else: col -= 1 right = 0 up = 1 while up and count < m*n: output += str(seats[row][col])+" " count += 1 if col > round + 1: col -= 1 else: row += 1 up = 0 left = 1 round += 1 print(output)
import java.util.*; public class Main{ public static void main(String args[]){ Scanner scan = new Scanner(System.in); while(scan.hasNext()){ int N = scan.nextInt(); int M = scan.nextInt(); int nums[][] = new int[N][M]; for(int i=0;i<N;i++){ for(int j=0;j<M;j++){ nums[i][j] = scan.nextInt(); } } int c = N<M?N:M; if((c&1)==1){ c = c/2+1; }else{ c=c/2; } int x,y; //一定要用ArrayList,不能用LinkedList,链表查找比较慢 ArrayList<Integer> res = new ArrayList<>(); for(int a=0;a<c;a++){ for(x=a;x<N-a;x++){ res.add(nums[x][a]); } for(y=a+1;y<M-a;y++){ res.add(nums[N-a-1][y]); } for(x=N-a-2;x>=a && (M-a-1)!=a ;x--){ res.add(nums[x][M-a-1]); } for(y=M-a-2;y>a && (N-a-1)!=a;y--){ res.add(nums[a][y]); } } for(x=0;x<res.size();x++){ if(x==res.size()-1){ System.out.print(res.get(x)); }else{ System.out.print(res.get(x)+" "); } } } } }
import java.util.ArrayList; import java.util.Scanner; public class Main { //打印路径 public static void main(String []args){ Scanner scan = new Scanner(System.in); ArrayList<Integer> arr = new ArrayList<Integer>(); //行 int m = scan.nextInt(); //列 int n = scan.nextInt(); scan.nextLine(); int result[][] = new int[m][n]; for(int i = 0; i < m; i++){ for(int j = 0; j < n; j++){ result[i][j] = scan.nextInt(); } } int left=0,right = n-1,up = 0,down = m-1; while(true){ //最左边一列 for(int i=up;i<=down;i++){ arr.add(result[i][left]); } left++; if(left>right){ break; } //最下面一行 for(int i = left;i<=right;i++){ arr.add(result[down][i]); } down--; if(down<up){ break; } //最右面一列 for(int i = down;i>=up;i--){ arr.add(result[i][right]); } right--; if(right<left){ break; } //最上面 for(int i = right;i>=left;i--){ arr.add(result[up][i]); } up++; if(up>down){ break; } } for(int i=0;i<arr.size();i++){ if(i==arr.size()-1){ System.out.print(arr.get(i)); }else{ System.out.print(arr.get(i)+" "); } } } }
#include<bits/stdc++.h> using namespace std; int main(){ int N,M; cin>>N>>M; int F[N][M]; for(int i=0;i<N;i++){ for(int j=0;j<M;j++){ cin>>F[i][j]; } } int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1}; vector<int> ans; int X=0,Y=0,k=0; for(int i=0;i<N*M;i++){ ans.push_back(F[X][Y]); F[X][Y] = 0; int a = X + dx[k%4],b = Y + dy[k%4]; if(a<0||a>=N||b<0||b>=M||F[a][b]==0){ k++; a = X + dx[k%4]; b = Y + dy[k%4]; } X = a; Y = b; } for(int i=0;i<N*M;i++){ cout<<ans[i]<<' '; } cout<<endl; }
#include <stdio.h> int main(void) { /*输入所要生成矩阵行数、列数 */ int Hang,Lie; scanf("%d %d",&Hang,&Lie); /*矩阵存放*/ int ShuZu[Hang][Lie]; //存放原始矩阵 int PaiXu[Hang*Lie]; //存放蛇形遍历矩阵数据 int i,j; //循环变量 /*输入编号*/ for(i=0;i<Hang;i++) { for(j=0;j<Lie;j++) { scanf("%d",&ShuZu[i][j]); } } /*变换矩阵将其螺旋解开(四层for循环)*/ //先清零排序后数组 for(i=0;i<Hang*Lie;i++) PaiXu[i] = 0; int Index = 0; int hang = Hang; int lie = Lie; i=0; j=0; while(Index!=Hang*Lie){ for(;i<hang;i++) { if(Index==Hang*Lie) continue; PaiXu[Index++] = ShuZu[i][j]; } i--; j++; for(;j<lie;j++) { if(Index==Hang*Lie) continue; PaiXu[Index++] = ShuZu[i][j]; } j--; i--; for(;i>=Lie-lie;i--) { if(Index==Hang*Lie) continue; PaiXu[Index++] = ShuZu[i][j]; } i++; j--; for(;j>Hang-hang;j--) { if(Index==Hang*Lie) continue; PaiXu[Index++] = ShuZu[i][j]; } j++; i++; hang--; lie--; } /*输出新元素*/ for(i=0;i<Hang*Lie;i++) { printf("%d ",PaiXu[i]); } return 0; }
#include <iostream> #include <cstring> using namespace std; int main(){ int n, m; cin>>n>>m; int matrix[n][m]; memset(matrix,0,sizeof(matrix)); for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ cin>>matrix[i][j]; } } int num = n*m; int nn = n; int mm = m; int i = 0, j = 0; while(num){ for(;i<nn;i++){ cout << matrix[i][j] << " "; num--; if(num==0) return 0; } i--; j++; for(;j<mm;j++){ cout << matrix[i][j] << " "; num--; if(num==0) return 0; } j--; i--; for(;i>=n-nn;i--){ cout << matrix[i][j] << " "; num--; if(num==0) return 0; } i++; j--; for(;j>m-mm;j--){ cout << matrix[i][j] << " "; num--; if(num==0) return 0; } j++; i++; mm--; nn--; } return 0; }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner input; int N, M, i, j; int[][] grid; input = new Scanner(System.in); while(input.hasNext()){ N = input.nextInt(); M = input.nextInt(); grid = new int[N][M]; for(i = 0; i < N; i++){ for(j = 0; j < M; j++){ grid[i][j] = input.nextInt(); } } System.out.println(new Main().Solution(grid, N, M)); } } private String Solution(int[][] grid, int N, int M){ int i, j, k, L,delta; StringBuilder ans; ans = new StringBuilder(); delta = 0; while(N > 0 && M > 0){ if(N == 1 || M == 1) L = N + M - 1; else L = (N + M) * 2 - 4; i = 0; j = 0; for(k = 0; k < L; k++){ ans.append(grid[i+delta][j+delta]); ans.append(" "); if(i < N - 1 && j == 0){ i++; }else if(i == N - 1 && j < M - 1){ j++; }else if(i > 0 && j == M - 1){ i--; }else if(i == 0 && j > 0){ j--; } } N -= 2; M -= 2; delta++; } return ans.toString(); } }